diff --git a/src/Client.php b/src/Client.php index c6d2c91..93ea8ab 100644 --- a/src/Client.php +++ b/src/Client.php @@ -66,7 +66,7 @@ class Client public const COMMAND_END_SESSIONS = "endSessions"; public const COMMAND_LIST_INDEXES = "listIndexes"; public const COMMAND_COLLMOD = "collMod"; - + public const COMMAND_KILL_CURSORS = "killCursors"; // Connection and performance settings private int $defaultMaxTimeMS = 30000; // 30 seconds default @@ -97,6 +97,14 @@ class Client public const READ_PREFERENCE_SECONDARY_PREFERRED = 'secondaryPreferred'; public const READ_PREFERENCE_NEAREST = 'nearest'; + /** + * Commands that do not support readConcern options + */ + private array $readConcernNotSupportedCommands = [ + self::COMMAND_GET_MORE, + self::COMMAND_KILL_CURSORS + ]; + /** * Authentication for connection @@ -320,7 +328,15 @@ public function query(array $command, ?string $db = null): stdClass|array|int // CRITICAL: Remove readConcern from any non-first operation in a transaction // MongoDB will reject commands with readConcern that have txnNumber but not startTransaction - if (isset($command['txnNumber']) && !isset($command['startTransaction']) && isset($command['readConcern'])) { + // Or if the command is in the readConcernNotSupportedCommands array + if ( + ( + isset($command['txnNumber']) + && !isset($command['startTransaction']) + && isset($command['readConcern']) + ) + || \in_array(array_key_first($command) ?? '', $this->readConcernNotSupportedCommands) + ) { unset($command['readConcern']); } diff --git a/tests/TransactionTest.php b/tests/TransactionTest.php index 1ae7643..b4cb651 100644 --- a/tests/TransactionTest.php +++ b/tests/TransactionTest.php @@ -162,7 +162,6 @@ public function testTransactionStateManagement() // Verify final state $finalState = $client->getSessionState($session); $this->assertEquals('committed', $finalState['state']); - } finally { $client->endSessions([$session]); } @@ -202,7 +201,6 @@ public function testTransactionAbort() // Verify document was not inserted (transaction rolled back) $found = $client->find('test_collection', ['name' => 'abort_test']); $this->assertEmpty($found->cursor->firstBatch); - } finally { $client->endSessions([$session]); } @@ -251,7 +249,6 @@ public function testWithTransactionHelper() $this->assertNotEmpty($found1->cursor->firstBatch); $this->assertNotEmpty($found2->cursor->firstBatch); - } finally { $client->endSessions([$session]); } @@ -343,7 +340,6 @@ public function testCRUDWithSessionAndConcerns() $this->assertEquals(1, $count); $client->commitTransaction($session); - } finally { $client->endSessions([$session]); }