diff --git a/composer.lock b/composer.lock index 24ee8e730f..444eb47507 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5c611cfa67003afadc6c7afff9166100", + "content-hash": "66c3842bf94f4946d1277b41cf28219d", "packages": [ { "name": "mongodb/mongodb", @@ -2317,7 +2317,7 @@ "prefer-lowest": false, "platform": { "php": ">=8.0", - "ext-mongodb": "^2.1" + "ext-mongodb": "2.1.1" }, "platform-dev": [], "plugin-api-version": "2.2.0" diff --git a/src/Client.php b/src/Client.php index 04696c3e43..164a700d94 100644 --- a/src/Client.php +++ b/src/Client.php @@ -37,6 +37,11 @@ class Client public const COMMAND_AGGREGATE = "aggregate"; public const COMMAND_DISTINCT = "distinct"; public const COMMAND_MAP_REDUCE = "mapReduce"; + public const COMMAND_START_SESSION = "startSession"; + public const COMMAND_COMMIT_TRANSACTION = "commitTransaction"; + public const COMMAND_ABORT_TRANSACTION = "abortTransaction"; + public const COMMAND_END_SESSIONS = "endSessions"; + /** * Authentication for connection @@ -732,6 +737,100 @@ class Client ]); } + /** + * Start a new logical session. Returns the session id object.. + * + * @return object + * @throws Exception + */ + public function startSession(): object + { + $result = $this->query([ + self::COMMAND_START_SESSION => 1 + ], 'admin'); + + return $result->id->id; + } + + /** + * Commit a transaction. + * + * @param array $lsid + * @param int $txnNumber + * @param bool $autocommit + * @return mixed + * @throws Exception + */ + public function commitTransaction(array $lsid, int $txnNumber, bool $autocommit = false) + { + $txnNumber = new \MongoDB\BSON\Int64($txnNumber); + + $result = $this->query([ + self::COMMAND_COMMIT_TRANSACTION => 1, + 'lsid' => $lsid, + 'txnNumber' => $txnNumber, + 'autocommit' => $autocommit + ], 'admin'); + + // End the session after successful commit + $this->endSessions([$lsid]); + + return $result; + } + + /** + * Abort (rollback) a transaction. + * + * @param array $lsid + * @param int $txnNumber + * @param bool $autocommit + * @return mixed + * @throws Exception + */ + public function abortTransaction(array $lsid, int $txnNumber, bool $autocommit = false) + { + $txnNumber = new \MongoDB\BSON\Int64($txnNumber); + + $result = $this->query([ + self::COMMAND_ABORT_TRANSACTION => 1, + 'lsid' => $lsid, + 'txnNumber' => $txnNumber, + 'autocommit' => $autocommit + ], 'admin'); + + // End the session after successful rollback + $this->endSessions([$lsid]); + + return $result; + } + + /** + * End sessions. + * + * @param array $lsids + * @param array $options + * @return mixed + * @throws Exception + */ + public function endSessions(array $lsids, array $options = []) + { + // Extract session IDs from the format ['id' => sessionId] and format as objects + $sessionIds = array_map(function ($lsid) { + $sessionId = $lsid['id'] ?? $lsid; + return ['id' => $sessionId]; + }, $lsids); + + return $this->query( + array_merge( + [ + self::COMMAND_END_SESSIONS => $sessionIds, + ], + $options + ), + 'admin' + ); + } + /** * Convert an assoc array to an object (stdClass). *