Skip to content

Commit 762f917

Browse files
committed
Adds transaction support
1 parent f5a95ba commit 762f917

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-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;
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 Session|null 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 Session $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: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace yii\mongodb;
99

1010
use MongoDB\Driver\Manager;
11+
use MongoDB\Driver\Session;
1112
use yii\base\Component;
1213
use yii\base\InvalidConfigException;
1314
use Yii;
@@ -181,6 +182,10 @@ class Connection extends Component
181182
*/
182183
private $_fileStreamWrapperRegistered = false;
183184

185+
/**
186+
* @var \MongoDB\Driver\Session|null session
187+
*/
188+
private $_session;
184189

185190
/**
186191
* Sets default database name.
@@ -408,11 +413,17 @@ protected function initConnection()
408413
*/
409414
public function createCommand($document = [], $databaseName = null)
410415
{
411-
return new Command([
416+
$command = new Command([
412417
'db' => $this,
413418
'databaseName' => $databaseName,
414419
'document' => $document,
415420
]);
421+
422+
if ($this->_session) {
423+
$command->setSession($this->_session);
424+
}
425+
426+
return $command;
416427
}
417428

418429
/**
@@ -432,4 +443,33 @@ public function registerFileStreamWrapper($force = false)
432443

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

0 commit comments

Comments
 (0)