10000 Transaction support (version 3.8) by haiquang9994 · Pull Request #2381 · mongodb/laravel-mongodb · GitHub
[go: up one dir, main page]

Skip to content

Transaction support (version 3.8) #2381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

class Connection extends BaseConnection
{
/**
* @var \MongoDB\Driver\Session
*/
protected $session;

/**
* The MongoDB database handler.
* @var \MongoDB\Database
Expand Down Expand Up @@ -287,4 +292,38 @@ public function __call($method, $parameters)
{
return call_user_func_array([$this->db, $method], $parameters);
}

public function getSession()
{
return $this->session;
}

protected function clearSession()
{
$this->session = null;
}

public function beginTransaction(array $options = [])
{
if (!$this->getSession()) {
$this->session = $this->getMongoClient()->startSession();
$this->session->startTransaction($options);
}
}

public function rollBack($toLevel = null)
{
if ($this->getSession()) {
$this->session->abortTransaction();
$this->clearSession();
}
}

public function commit()
{
if ($this->getSession()) {
$this->session->commitTransaction();
$this->clearSession();
}
}
}
33 changes: 30 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,11 @@ public function insert(array $values)
}

// Batch insert
$result = $this->collection->insertMany($values);
$options = [];
if ($session = $this->getSession()) {
$options['session'] = $session;
}
$result = $this->collection->insertMany($values, $options);

return 1 == (int) $result->isAcknowledged();
}
Expand All @@ -578,7 +582,11 @@ public function insert(array $values)
*/
public function insertGetId(array $values, $sequence = null)
{
$result = $this->collection->insertOne($values);
$options = [];
if ($session = $this->getSession()) {
$options['session'] = $session;
}
$result = $this->collection->insertOne($values, $options);

if (1 == (int) $result->isAcknowledged()) {
if ($sequence === null) {
Expand Down Expand Up @@ -682,7 +690,11 @@ public function delete($id = null)
}

$wheres = $this->compileWheres();
$result = $this->collection->DeleteMany($wheres);
$options = [];
if ($session = $this->getSession()) {
$options['session'] = $session;
}
$result = $this->collection->DeleteMany($wheres, $options);
if (1 == (int) $result->isAcknowledged()) {
return $result->getDeletedCount();
}
Expand Down Expand Up @@ -836,6 +848,9 @@ protected function performUpdate($query, array $options = [])
}

$wheres = $this->compileWheres();
if ($session = $this->getSession()) {
$options['session'] = $session;
}
$result = $this->collection->UpdateMany($wheres, $query, $options);
if (1 == (int) $result->isAcknowledged()) {
return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount();
Expand Down Expand Up @@ -1237,4 +1252,16 @@ public function __call($method, $parameters)

return parent::__call($method, $parameters);
}

/**
* @return \MongoDB\Driver\Session|null
*/
protected function getSession()
{
if ($session = $this->connection->getSession()) {
return $session;
}

return null;
}
}
0