Skip to content
Open
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
26 changes: 24 additions & 2 deletions lib/Connection.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ abstract class Connection
*/
static $DEFAULT_PORT = 0;

/**
* Number of nested transactions
* @var int
*/
static protected $transaction_counter = 0;

/**
* Retrieve a database connection.
*
Expand Down Expand Up @@ -376,15 +382,26 @@ public function tables()
*/
public function transaction()
{
if (!$this->connection->beginTransaction())
throw new DatabaseException($this);
// Transaction started already
if (static::$transaction_counter++) {
$this->connection->exec('SAVEPOINT trans'.static::$transaction_counter);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will never be a trans0 like this, which I think is kinda odd.

return;
}

if (!$this->connection->beginTransaction())
throw new DatabaseException($this);
}

/**
* Commits the current transaction.
*/
public function commit()
{
// Nested transaction simply decrease number
if (--static::$transaction_counter) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return;
}

if (!$this->connection->commit())
throw new DatabaseException($this);
}
Expand All @@ -394,6 +411,11 @@ public function commit()
*/
public function rollback()
{
if (--static::$transaction_counter) {
$this->connection->exec('ROLLBACK TO trans'.(static::$transaction_counter + 1));
return true;
}

if (!$this->connection->rollback())
throw new DatabaseException($this);
}
Expand Down
29 changes: 29 additions & 0 deletions test/helpers/AdapterTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,34 @@ public function test_date_to_string()
$datetime = '2009-01-01';
$this->assert_equals($datetime,$this->conn->date_to_string(date_create($datetime)));
}


public function test_transaction_nested_commit()
{
$original = $this->conn->query_and_fetch_one("select count(*) from authors");

$this->conn->transaction();
$this->conn->query("insert into authors(author_id,name) values(9998,'blahhhhhhhh')");
// Nested
$this->conn->transaction();
$this->conn->query("insert into authors(author_id,name) values(9999,'blahhhhhhhh')");
$this->conn->commit();

// Another nested
try {
$this->conn->transaction();
$this->conn->query("insert into authors(author_id,name) values(9999,'blahhhhhhhh')"); // fails
$this->conn->commit();
}
catch (Exception $e)
{
$this->conn->rollback();
}

// Commit primary transaction
$this->conn->commit();

$this->assert_equals($original+2, $this->conn->query_and_fetch_one("select count(*) from authors"));
}
}
?>