完善事务嵌套

This commit is contained in:
yunwuxin
2016-06-15 11:03:08 +08:00
parent ea8bad7759
commit d08ae3671a
5 changed files with 80 additions and 35 deletions

View File

@@ -36,8 +36,6 @@ abstract class Connection
protected $numRows = 0; protected $numRows = 0;
// 事务指令数 // 事务指令数
protected $transTimes = 0; protected $transTimes = 0;
// 事务标识
protected $transLabel = '';
// 错误信息 // 错误信息
protected $error = ''; protected $error = '';
@@ -520,65 +518,67 @@ abstract class Connection
* @access public * @access public
* @param callable $callback 数据操作方法回调 * @param callable $callback 数据操作方法回调
* @return mixed * @return mixed
* @throws PDOException
* @throws \Exception
* @throws \Throwable
*/ */
public function transaction($callback) public function transaction($callback)
{ {
$label = microtime(true); $this->startTrans();
$this->startTrans($label);
try { try {
$result = null; $result = null;
if (is_callable($callback)) { if (is_callable($callback)) {
$result = call_user_func_array($callback, []); $result = call_user_func_array($callback, [$this]);
} }
$this->commit($label); $this->commit();
return $result; return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
$this->rollback(); $this->rollback();
throw $e; throw $e;
} catch (\Throwable $e) {
$this->rollback();
throw $e;
} }
} }
/** /**
* 启动事务 * 启动事务
* @access public * @access public
* @param string $label 事务标识
* @return bool|null * @return bool|null
*/ */
public function startTrans($label = '') public function startTrans()
{ {
$this->initConnect(true); $this->initConnect(true);
if (!$this->linkID) { if (!$this->linkID) {
return false; return false;
} }
//数据rollback 支持 ++$this->transTimes;
if (0 == $this->transTimes) {
$this->transLabel = $label; if ($this->transTimes == 1) {
$this->linkID->beginTransaction(); $this->linkID->beginTransaction();
} elseif ($this->transTimes > 1 && $this->supportSavepoint()) {
$this->linkID->exec(
$this->parseSavepoint('trans' . $this->transTimes)
);
} }
$this->transTimes++;
return;
} }
/** /**
* 用于非自动提交状态下面的查询提交 * 用于非自动提交状态下面的查询提交
* @access public * @access public
* @param string $label 事务标识
* @return boolean * @return boolean
* @throws PDOException * @throws PDOException
*/ */
public function commit($label = '') public function commit()
{ {
$this->initConnect(true); $this->initConnect(true);
if ($this->transTimes > 0 && $label == $this->transLabel) {
try { if ($this->transTimes == 1) {
$this->linkID->commit(); $this->linkID->commit();
$this->transTimes = 0;
} catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr);
}
} }
return true;
--$this->transTimes;
} }
/** /**
@@ -590,15 +590,45 @@ abstract class Connection
public function rollback() public function rollback()
{ {
$this->initConnect(true); $this->initConnect(true);
if ($this->transTimes > 0) {
try { if ($this->transTimes == 1) {
$this->linkID->rollback(); $this->linkID->rollBack();
$this->transTimes = 0; } elseif ($this->transTimes > 1 && $this->supportSavepoint()) {
} catch (\PDOException $e) { $this->linkID->exec(
throw new PDOException($e, $this->config, $this->queryStr); $this->parseSavepointRollBack('trans' . $this->transTimes)
} );
} }
return true;
$this->transTimes = max(0, $this->transTimes - 1);
}
/**
* 是否支持事务嵌套
* @return bool
*/
protected function supportSavepoint()
{
return false;
}
/**
* 生成定义保存点的SQL
* @param $name
* @return string
*/
protected function parseSavepoint($name)
{
return 'SAVEPOINT ' . $name;
}
/**
* 生成回滚到保存点的SQL
* @param $name
* @return string
*/
protected function parseSavepointRollBack($name)
{
return 'ROLLBACK TO SAVEPOINT ' . $name;
} }
/** /**
@@ -614,14 +644,13 @@ abstract class Connection
return false; return false;
} }
// 自动启动事务支持 // 自动启动事务支持
$label = microtime(true); $this->startTrans();
$this->startTrans($label);
try { try {
foreach ($sqlArray as $sql) { foreach ($sqlArray as $sql) {
$result = $this->execute($sql); $this->execute($sql);
} }
// 提交事务 // 提交事务
$this->commit($label); $this->commit();
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->rollback(); $this->rollback();
return false; return false;

View File

@@ -110,4 +110,8 @@ class Mysql extends Connection
} }
return $result; return $result;
} }
protected function supportSavepoint(){
return true;
}
} }

View File

@@ -154,4 +154,8 @@ class Oracle extends Connection
{ {
return []; return [];
} }
protected function supportSavepoint(){
return true;
}
} }

View File

@@ -93,4 +93,8 @@ class Pgsql extends Connection
{ {
return []; return [];
} }
protected function supportSavepoint(){
return true;
}
} }

View File

@@ -92,4 +92,8 @@ class Sqlite extends Connection
{ {
return []; return [];
} }
protected function supportSavepoint(){
return true;
}
} }