mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 13:17:58 +02:00
Connection: begin(), commit() & rollback() calls are forbidden in transaction()
This commit is contained in:
committed by
David Grudl
parent
b00e556289
commit
3066fea2aa
@@ -337,6 +337,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
public function begin(string $savepoint = null): void
|
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) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
@@ -361,6 +365,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
public function commit(string $savepoint = null): void
|
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) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
@@ -385,6 +393,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
public function rollback(string $savepoint = null): void
|
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) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
|
@@ -108,3 +108,24 @@ test('nested transaction() call success', function () use ($conn) {
|
|||||||
});
|
});
|
||||||
Assert::same(7, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
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');
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user