1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-01 11:50:15 +02:00

Connection: begin(), commit() & rollback() calls are forbidden in transaction()

This commit is contained in:
Miloslav Hůla
2021-04-04 22:54:04 +02:00
committed by David Grudl
parent b00e556289
commit 3066fea2aa
2 changed files with 33 additions and 0 deletions

View File

@@ -337,6 +337,10 @@ class Connection implements IConnection
*/
public function begin(string $savepoint = null): void
{
if ($this->transactionDepth !== 0) {
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
}
if (!$this->driver) {
$this->connect();
}
@@ -361,6 +365,10 @@ class Connection implements IConnection
*/
public function commit(string $savepoint = null): void
{
if ($this->transactionDepth !== 0) {
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
}
if (!$this->driver) {
$this->connect();
}
@@ -385,6 +393,10 @@ class Connection implements IConnection
*/
public function rollback(string $savepoint = null): void
{
if ($this->transactionDepth !== 0) {
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
}
if (!$this->driver) {
$this->connect();
}

View File

@@ -108,3 +108,24 @@ test('nested transaction() call success', function () use ($conn) {
});
Assert::same(7, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
});
test('begin(), commit() & rollback() calls are forbidden in transaction()', function () use ($conn) {
Assert::exception(function () use ($conn) {
$conn->transaction(function (Dibi\Connection $connection) {
$connection->begin();
});
}, \LogicException::class, Dibi\Connection::class . '::begin() call is forbidden inside a transaction() callback');
Assert::exception(function () use ($conn) {
$conn->transaction(function (Dibi\Connection $connection) {
$connection->commit();
});
}, \LogicException::class, Dibi\Connection::class . '::commit() call is forbidden inside a transaction() callback');
Assert::exception(function () use ($conn) {
$conn->transaction(function (Dibi\Connection $connection) {
$connection->rollback();
});
}, \LogicException::class, Dibi\Connection::class . '::rollback() call is forbidden inside a transaction() callback');
});