Skip to content

Commit

Permalink
支持强制回滚模式
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Sep 12, 2024
1 parent ca0fc4a commit 28a6983
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Config extends \EasySwoole\Pool\Config

protected bool $useMysqli = false;

protected bool $isForceRollback = false;

/**
* @return string
*/
Expand Down Expand Up @@ -172,4 +174,14 @@ public function setUseMysqli(bool $useMysqli): void
{
$this->useMysqli = $useMysqli;
}

public function isIsForceRollback(): bool
{
return $this->isForceRollback;
}

public function setIsForceRollback(bool $isForceRollback): void
{
$this->isForceRollback = $isForceRollback;
}
}
6 changes: 4 additions & 2 deletions src/Mysql/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class Connection extends Client implements ObjectInterface
public string $connectionName;
public bool $isInTransaction = false;

public bool $isForceRollback = false;

public int $lastPingTime = 0;

function gc()
{
if($this->isInTransaction){
if($this->isInTransaction || $this->isForceRollback){
try {
$this->mysqlClient()->rollback();
}catch (\Throwable $throwable){
Expand All @@ -28,7 +30,7 @@ function gc()

function objectRestore()
{
if($this->isInTransaction){
if($this->isInTransaction || $this->isForceRollback){
try {
$this->mysqlClient()->rollback();
}catch (\Throwable $throwable){
Expand Down
4 changes: 3 additions & 1 deletion src/Mysql/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Pool extends AbstractPool
protected function createObject()
{
$config = new Config($this->getConfig()->toArray());
$con = new Connection($config);
$con = new Connection($config);
$con->isForceRollback = $this->getConfig()->isIsForceRollback();

if(!$con->connect()){
$info = $con->mysqlClient()->connect_error;
/** @var \EasySwoole\FastDb\Config $config */
Expand Down

0 comments on commit 28a6983

Please sign in to comment.