9090from bson .py3compat import abc , reraise_instance
9191from bson .timestamp import Timestamp
9292
93- from pymongo import monotonic
93+ from pymongo import monotonic , __version__
9494from pymongo .errors import (ConfigurationError ,
9595 ConnectionFailure ,
9696 InvalidOperation ,
@@ -263,6 +263,10 @@ def _reraise_with_unknown_commit(exc):
263263 64 , # WriteConcernFailed
264264])
265265
266+ _MONGOS_NOT_SUPPORTED_MSG = (
267+ 'PyMongo %s does not support running multi-document transactions on '
268+ 'sharded clusters' ) % (__version__ ,)
269+
266270
267271class ClientSession (object ):
268272 """A session for ordering sequential operations."""
@@ -356,6 +360,9 @@ def start_transaction(self, read_concern=None, write_concern=None,
356360 """
357361 self ._check_ended ()
358362
363+ if self ._client ._is_mongos_non_blocking ():
364+ raise ConfigurationError (_MONGOS_NOT_SUPPORTED_MSG )
365+
359366 if self ._in_transaction :
360367 raise InvalidOperation ("Transaction already in progress" )
361368
@@ -534,7 +541,7 @@ def _txn_read_preference(self):
534541 return self ._transaction .opts .read_preference
535542 return None
536543
537- def _apply_to (self , command , is_retryable , read_preference ):
544+ def _apply_to (self , command , is_retryable , read_preference , sock_info ):
538545 self ._check_ended ()
539546
540547 self ._server_session .last_use = monotonic .time ()
@@ -548,6 +555,9 @@ def _apply_to(self, command, is_retryable, read_preference):
548555 return
549556
550557 if self ._in_transaction :
558+ if sock_info .is_mongos :
559+ raise ConfigurationError (_MONGOS_NOT_SUPPORTED_MSG )
560+
551561 if read_preference != ReadPreference .PRIMARY :
552562 raise InvalidOperation (
553563 'read preference in a transaction must be primary, not: '
0 commit comments