This commit is contained in:
as2252258@163.com
2021-02-21 12:58:21 +08:00
parent 08ac31267b
commit 8a40bd7130
+12 -6
View File
@@ -6,10 +6,12 @@
* Time: 15:40 * Time: 15:40
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace Database; namespace Database;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
use Exception; use Exception;
use HttpServer\Http\Context;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -20,14 +22,12 @@ class Db
{ {
use QueryTrait; use QueryTrait;
private static bool $isActive = false;
/** /**
* @return bool * @return bool
*/ */
public static function transactionsActive(): bool public static function transactionsActive(): bool
{ {
return static::$isActive; return Context::getContext('begin:transaction') === true;
} }
/** /**
@@ -35,7 +35,7 @@ class Db
*/ */
public static function beginTransaction() public static function beginTransaction()
{ {
static::$isActive = true; Context::setContext('begin:transaction', true);
} }
/** /**
@@ -43,10 +43,13 @@ class Db
*/ */
public static function commit() public static function commit()
{ {
if (!static::transactionsActive()) {
return;
}
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->trigger(Connection::TRANSACTION_COMMIT); $event->trigger(Connection::TRANSACTION_COMMIT);
$event->offName(Connection::TRANSACTION_COMMIT); $event->offName(Connection::TRANSACTION_COMMIT);
static::$isActive = false; Context::deleteId('begin:transaction');
} }
/** /**
@@ -54,10 +57,13 @@ class Db
*/ */
public static function rollback() public static function rollback()
{ {
if (!static::transactionsActive()) {
return;
}
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->trigger(Connection::TRANSACTION_ROLLBACK); $event->trigger(Connection::TRANSACTION_ROLLBACK);
$event->offName(Connection::TRANSACTION_ROLLBACK); $event->offName(Connection::TRANSACTION_ROLLBACK);
static::$isActive = false; Context::deleteId('begin:transaction');
} }
/** /**