|
52 | 52 | from bson.timestamp import Timestamp |
53 | 53 |
|
54 | 54 | from pymongo import monotonic |
55 | | -from pymongo.errors import ConnectionFailure, InvalidOperation, OperationFailure |
| 55 | +from pymongo.errors import (ConnectionFailure, |
| 56 | + InvalidOperation, |
| 57 | + OperationFailure) |
| 58 | +from pymongo.read_preferences import ReadPreference |
56 | 59 |
|
57 | 60 |
|
58 | 61 | class SessionOptions(object): |
@@ -127,8 +130,6 @@ class _Transaction(object): |
127 | 130 | """Internal class to hold transaction information in a ClientSession.""" |
128 | 131 | def __init__(self, opts): |
129 | 132 | self.opts = opts |
130 | | - self.read_preference = None |
131 | | - self.address = None |
132 | 133 |
|
133 | 134 |
|
134 | 135 | class ClientSession(object): |
@@ -259,7 +260,7 @@ def _finish_transaction(self, command_name): |
259 | 260 | stmtId=self._server_session.statement_id, |
260 | 261 | session=self, |
261 | 262 | write_concern=write_concern, |
262 | | - read_preference=self._transaction.read_preference, |
| 263 | + read_preference=ReadPreference.PRIMARY, |
263 | 264 | parse_write_concern_error=True) |
264 | 265 | finally: |
265 | 266 | self._server_session.reset_transaction() |
@@ -334,18 +335,20 @@ def _apply_to(self, command, is_retryable, read_preference): |
334 | 335 | return |
335 | 336 |
|
336 | 337 | if self.in_transaction: |
| 338 | + if read_preference != ReadPreference.PRIMARY: |
| 339 | + raise InvalidOperation( |
| 340 | + 'read preference in a transaction must be primary, not: ' |
| 341 | + '%r' % (read_preference,)) |
| 342 | + |
337 | 343 | if self._server_session.statement_id == 0: |
338 | 344 | # First statement begins a new transaction. |
339 | | - self._transaction.read_preference = read_preference |
340 | 345 | self._server_session._transaction_id += 1 |
341 | 346 | command['startTransaction'] = True |
342 | 347 | read_concern = command.setdefault('readConcern', {}) |
343 | 348 | read_concern['level'] = 'snapshot' |
344 | 349 | if (self.options.causal_consistency |
345 | 350 | and self.operation_time is not None): |
346 | 351 | read_concern['afterClusterTime'] = self.operation_time |
347 | | - elif read_preference != self._transaction.read_preference: |
348 | | - raise InvalidOperation('Transaction readPreference changed') |
349 | 352 |
|
350 | 353 | command['txnNumber'] = self._server_session.transaction_id |
351 | 354 | command['stmtId'] = self._server_session.statement_id |
|
0 commit comments