@@ -184,14 +184,16 @@ def __init__(self, database, name, create=False, codec_options=None,
184184 unicode_decode_error_handler = 'replace' ,
185185 document_class = dict )
186186
187- def _socket_for_reads (self ):
188- return self .__database .client ._socket_for_reads (self .read_preference )
187+ def _socket_for_reads (self , session ):
188+ return self .__database .client ._socket_for_reads (
189+ self .read_preference , session )
189190
190- def _socket_for_primary_reads (self ):
191- return self .__database .client ._socket_for_reads (ReadPreference .PRIMARY )
191+ def _socket_for_primary_reads (self , session ):
192+ return self .__database .client ._socket_for_reads (
193+ ReadPreference .PRIMARY , session )
192194
193- def _socket_for_writes (self ):
194- return self .__database .client ._socket_for_writes ()
195+ def _socket_for_writes (self , session ):
196+ return self .__database .client ._socket_for_writes (session )
195197
196198 def _command (self , sock_info , command , slave_ok = False ,
197199 read_preference = None ,
@@ -252,7 +254,7 @@ def __create(self, options, collation, session):
252254 if "size" in options :
253255 options ["size" ] = float (options ["size" ])
254256 cmd .update (options )
255- with self ._socket_for_writes () as sock_info :
257+ with self ._socket_for_writes (session ) as sock_info :
256258 self ._command (
257259 sock_info , cmd , read_preference = ReadPreference .PRIMARY ,
258260 write_concern = self .write_concern ,
@@ -579,7 +581,7 @@ def _insert_command(session, sock_info, retryable_write):
579581 True , _insert_command , session )
580582 _check_write_command_response (result )
581583 else :
582- with self ._socket_for_writes () as sock_info :
584+ with self ._socket_for_writes (session = None ) as sock_info :
583585 # Legacy OP_INSERT.
584586 self ._legacy_write (
585587 sock_info , 'insert' , command , op_id ,
@@ -1493,7 +1495,7 @@ def parallel_scan(self, num_cursors, session=None, **kwargs):
14931495 ('numCursors' , num_cursors )])
14941496 cmd .update (kwargs )
14951497
1496- with self ._socket_for_reads () as (sock_info , slave_ok ):
1498+ with self ._socket_for_reads (session ) as (sock_info , slave_ok ):
14971499 result = self ._command (sock_info , cmd , slave_ok ,
14981500 read_concern = self .read_concern ,
14991501 session = session )
@@ -1509,7 +1511,7 @@ def parallel_scan(self, num_cursors, session=None, **kwargs):
15091511
15101512 def _count (self , cmd , collation = None , session = None ):
15111513 """Internal count helper."""
1512- with self ._socket_for_reads () as (sock_info , slave_ok ):
1514+ with self ._socket_for_reads (session ) as (sock_info , slave_ok ):
15131515 res = self ._command (
15141516 sock_info , cmd , slave_ok ,
15151517 allowable_errors = ["ns missing" ],
@@ -1606,7 +1608,7 @@ def create_indexes(self, indexes, session=None, **kwargs):
16061608 """
16071609 common .validate_list ('indexes' , indexes )
16081610 names = []
1609- with self ._socket_for_writes () as sock_info :
1611+ with self ._socket_for_writes (session ) as sock_info :
16101612 supports_collations = sock_info .max_wire_version >= 5
16111613 def gen_indexes ():
16121614 for index in indexes :
@@ -1647,7 +1649,7 @@ def __create_index(self, keys, index_options, session, **kwargs):
16471649 index_options .pop ('collation' , None ))
16481650 index .update (index_options )
16491651
1650- with self ._socket_for_writes () as sock_info :
1652+ with self ._socket_for_writes (session ) as sock_info :
16511653 if collation is not None :
16521654 if sock_info .max_wire_version < 5 :
16531655 raise ConfigurationError (
@@ -1874,7 +1876,7 @@ def drop_index(self, index_or_name, session=None, **kwargs):
18741876 self .__database .name , self .__name , name )
18751877 cmd = SON ([("dropIndexes" , self .__name ), ("index" , name )])
18761878 cmd .update (kwargs )
1877- with self ._socket_for_writes () as sock_info :
1879+ with self ._socket_for_writes (session ) as sock_info :
18781880 self ._command (sock_info ,
18791881 cmd ,
18801882 read_preference = ReadPreference .PRIMARY ,
@@ -1911,7 +1913,7 @@ def reindex(self, session=None, **kwargs):
19111913 """
19121914 cmd = SON ([("reIndex" , self .__name )])
19131915 cmd .update (kwargs )
1914- with self ._socket_for_writes () as sock_info :
1916+ with self ._socket_for_writes (session ) as sock_info :
19151917 return self ._command (
19161918 sock_info , cmd , read_preference = ReadPreference .PRIMARY ,
19171919 parse_write_concern_error = True , session = session )
@@ -1940,7 +1942,7 @@ def list_indexes(self, session=None):
19401942 codec_options = CodecOptions (SON )
19411943 coll = self .with_options (codec_options = codec_options ,
19421944 read_preference = ReadPreference .PRIMARY )
1943- with self ._socket_for_primary_reads () as (sock_info , slave_ok ):
1945+ with self ._socket_for_primary_reads (session ) as (sock_info , slave_ok ):
19441946 cmd = SON ([("listIndexes" , self .__name ), ("cursor" , {})])
19451947 if sock_info .max_wire_version > 2 :
19461948 with self .__database .client ._tmp_session (session , False ) as s :
@@ -2061,7 +2063,7 @@ def _aggregate(self, pipeline, cursor_class, first_batch_size, session,
20612063 "batchSize" , kwargs .pop ("batchSize" , None ))
20622064 # If the server does not support the "cursor" option we
20632065 # ignore useCursor and batchSize.
2064- with self ._socket_for_reads () as (sock_info , slave_ok ):
2066+ with self ._socket_for_reads (session ) as (sock_info , slave_ok ):
20652067 dollar_out = pipeline and '$out' in pipeline [- 1 ]
20662068 if use_cursor :
20672069 if "cursor" not in kwargs :
@@ -2350,7 +2352,7 @@ def group(self, key, condition, initial, reduce, finalize=None, **kwargs):
23502352 collation = validate_collation_or_none (kwargs .pop ('collation' , None ))
23512353 cmd .update (kwargs )
23522354
2353- with self ._socket_for_reads () as (sock_info , slave_ok ):
2355+ with self ._socket_for_reads (session = None ) as (sock_info , slave_ok ):
23542356 return self ._command (sock_info , cmd , slave_ok ,
23552357 collation = collation )["retval" ]
23562358
@@ -2396,7 +2398,7 @@ def rename(self, new_name, session=None, **kwargs):
23962398
23972399 new_name = "%s.%s" % (self .__database .name , new_name )
23982400 cmd = SON ([("renameCollection" , self .__full_name ), ("to" , new_name )])
2399- with self ._socket_for_writes () as sock_info :
2401+ with self ._socket_for_writes (session ) as sock_info :
24002402 with self .__database .client ._tmp_session (session ) as s :
24012403 if sock_info .max_wire_version >= 5 and self .write_concern :
24022404 cmd ['writeConcern' ] = self .write_concern .document
@@ -2451,7 +2453,7 @@ def distinct(self, key, filter=None, session=None, **kwargs):
24512453 kwargs ["query" ] = filter
24522454 collation = validate_collation_or_none (kwargs .pop ('collation' , None ))
24532455 cmd .update (kwargs )
2454- with self ._socket_for_reads () as (sock_info , slave_ok ):
2456+ with self ._socket_for_reads (session ) as (sock_info , slave_ok ):
24552457 return self ._command (sock_info , cmd , slave_ok ,
24562458 read_concern = self .read_concern ,
24572459 collation = collation , session = session )["values" ]
@@ -2523,7 +2525,7 @@ def map_reduce(self, map, reduce, out, full_response=False, session=None,
25232525 cmd .update (kwargs )
25242526
25252527 inline = 'inline' in cmd ['out' ]
2526- with self ._socket_for_primary_reads () as (sock_info , slave_ok ):
2528+ with self ._socket_for_primary_reads (session ) as (sock_info , slave_ok ):
25272529 if (sock_info .max_wire_version >= 5 and self .write_concern and
25282530 not inline ):
25292531 cmd ['writeConcern' ] = self .write_concern .document
@@ -2592,7 +2594,7 @@ def inline_map_reduce(self, map, reduce, full_response=False, session=None,
25922594 ("out" , {"inline" : 1 })])
25932595 collation = validate_collation_or_none (kwargs .pop ('collation' , None ))
25942596 cmd .update (kwargs )
2595- with self ._socket_for_reads () as (sock_info , slave_ok ):
2597+ with self ._socket_for_reads (session ) as (sock_info , slave_ok ):
25962598 if sock_info .max_wire_version >= 4 and 'readConcern' not in cmd :
25972599 res = self ._command (sock_info , cmd , slave_ok ,
25982600 read_concern = self .read_concern ,
0 commit comments