Skip to content

Commit a81132c

Browse files
committed
Adds transaction support
1 parent 24422d4 commit a81132c

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/Command.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MongoDB\Driver\Exception\RuntimeException;
1313
use MongoDB\Driver\ReadConcern;
1414
use MongoDB\Driver\ReadPreference;
15+
use MongoDB\Driver\Session as DriverSession;
1516
use MongoDB\Driver\WriteConcern;
1617
use MongoDB\Driver\WriteResult;
1718
use Yii;
@@ -85,7 +86,10 @@ class Command extends BaseObject
8586
* @var ReadConcern|string read concern to be used by this command
8687
*/
8788
private $_readConcern;
88-
89+
/**
90+
* @var DriverSession session to be used by this command
91+
*/
92+
private $_session;
8993

9094
/**
9195
* Returns read preference for this command.
@@ -167,6 +171,17 @@ public function setReadConcern($readConcern)
167171
return $this;
168172
}
169173

174+
/**
175+
* Sets session for this command.
176+
* @param DriverSession $session session
177+
* @return $this self reference
178+
*/
179+
public function setSession($session)
180+
{
181+
$this->_session = $session;
182+
return $this;
183+
}
184+
170185
/**
171186
* Executes this command.
172187
* @return \MongoDB\Driver\Cursor result cursor.
@@ -183,7 +198,11 @@ public function execute()
183198

184199
$this->db->open();
185200
$mongoCommand = new \MongoDB\Driver\Command($this->document);
186-
$cursor = $this->db->manager->executeCommand($databaseName, $mongoCommand, $this->getReadPreference());
201+
$options = array_filter([
202+
'readPreference' => $this->getReadPreference(),
203+
'session' => $this->_session,
204+
]);
205+
$cursor = $this->db->manager->executeCommand($databaseName, $mongoCommand, $options);
187206
$cursor->setTypeMap($this->db->typeMap);
188207

189208
$this->endProfile($token, __METHOD__);
@@ -236,7 +255,11 @@ public function executeBatch($collectionName, $options = [])
236255
}
237256

238257
$this->db->open();
239-
$writeResult = $this->db->manager->executeBulkWrite($databaseName . '.' . $collectionName, $batch, $this->getWriteConcern());
258+
$options = array_filter([
259+
'writeConcern' => $this->getWriteConcern(),
260+
'session' => $this->_session,
261+
]);
262+
$writeResult = $this->db->manager->executeBulkWrite($databaseName . '.' . $collectionName, $batch, $options);
240263

241264
$this->endProfile($token, __METHOD__);
242265
} catch (RuntimeException $e) {
@@ -283,7 +306,11 @@ public function query($collectionName, $options = [])
283306

284307
$query = new \MongoDB\Driver\Query($this->document, $options);
285308
$this->db->open();
286-
$cursor = $this->db->manager->executeQuery($databaseName . '.' . $collectionName, $query, $this->getReadPreference());
309+
$options = array_filter([
310+
'readPreference' => $this->getReadPreference(),
311+
'session' => $this->_session,
312+
]);
313+
$cursor = $this->db->manager->executeQuery($databaseName . '.' . $collectionName, $query, $options);
287314
$cursor->setTypeMap($this->db->typeMap);
288315

289316
$this->endProfile($token, __METHOD__);

src/Connection.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ class Connection extends Component
181181
*/
182182
private $_fileStreamWrapperRegistered = false;
183183

184+
/**
185+
* @var \MongoDB\Driver\Session session
186+
*/
187+
private $_session;
184188

185189
/**
186190
* Sets default database name.
@@ -408,11 +412,17 @@ protected function initConnection()
408412
*/
409413
public function createCommand($document = [], $databaseName = null)
410414
{
411-
return new Command([
415+
$command = new Command([
412416
'db' => $this,
413417
'databaseName' => $databaseName,
414418
'document' => $document,
415419
]);
420+
421+
if ($this->_session) {
422+
$command->setSession($this->_session);
423+
}
424+
425+
return $command;
416426
}
417427

418428
/**
@@ -432,4 +442,33 @@ public function registerFileStreamWrapper($force = false)
432442

433443
return $this->fileStreamProtocol;
434444
}
445+
446+
/**
447+
* @see Session::startTransaction()
448+
*/
449+
public function startTransaction($options = [])
450+
{
451+
$this->_session = $this->manager->startSession();
452+
$this->_session->startTransaction($options);
453+
}
454+
455+
/**
456+
* @see Session::abortTransaction()
457+
*/
458+
public function abortTransaction()
459+
{
460+
$this->_session->abortTransaction();
461+
$this->_session->endSession();
462+
$this->_session = null;
463+
}
464+
465+
/**
466+
* @see Session::commitTransaction()
467+
*/
468+
public function commitTransaction()
469+
{
470+
$this->_session->commitTransaction();
471+
$this->_session->endSession();
472+
$this->_session = null;
473+
}
435474
}

0 commit comments

Comments
 (0)