改进事务的断线重连

This commit is contained in:
thinkphp
2017-05-19 11:27:23 +08:00
parent 23883590c4
commit c14988ee84

View File

@@ -382,12 +382,12 @@ abstract class Connection
// 返回结果集 // 返回结果集
return $this->getResult($pdo, $procedure); return $this->getResult($pdo, $procedure);
} catch (\PDOException $e) { } catch (\PDOException $e) {
if ($this->config['break_reconnect'] && $this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->query($sql, $bind, $master, $pdo); return $this->close()->query($sql, $bind, $master, $pdo);
} }
throw new PDOException($e, $this->config, $this->getLastsql()); throw new PDOException($e, $this->config, $this->getLastsql());
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
if ($this->config['break_reconnect'] && $this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->query($sql, $bind, $master, $pdo); return $this->close()->query($sql, $bind, $master, $pdo);
} }
throw $e; throw $e;
@@ -445,12 +445,12 @@ abstract class Connection
$this->numRows = $this->PDOStatement->rowCount(); $this->numRows = $this->PDOStatement->rowCount();
return $this->numRows; return $this->numRows;
} catch (\PDOException $e) { } catch (\PDOException $e) {
if ($this->config['break_reconnect'] && $this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind); return $this->close()->execute($sql, $bind);
} }
throw new PDOException($e, $this->config, $this->getLastsql()); throw new PDOException($e, $this->config, $this->getLastsql());
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
if ($this->config['break_reconnect'] && $this->isBreak($e)) { if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind); return $this->close()->execute($sql, $bind);
} }
throw $e; throw $e;
@@ -628,13 +628,25 @@ abstract class Connection
} }
++$this->transTimes; ++$this->transTimes;
try {
if (1 == $this->transTimes) {
$this->linkID->beginTransaction();
} elseif ($this->transTimes > 1 && $this->supportSavepoint()) {
$this->linkID->exec(
$this->parseSavepoint('trans' . $this->transTimes)
);
}
if (1 == $this->transTimes) { } catch (\PDOException $e) {
$this->linkID->beginTransaction(); if ($this->isBreak($e)) {
} elseif ($this->transTimes > 1 && $this->supportSavepoint()) { return $this->close()->startTrans();
$this->linkID->exec( }
$this->parseSavepoint('trans' . $this->transTimes) throw $e;
); } catch (\ErrorException $e) {
if ($this->isBreak($e)) {
return $this->close()->startTrans();
}
throw $e;
} }
} }
@@ -775,6 +787,10 @@ abstract class Connection
*/ */
protected function isBreak($e) protected function isBreak($e)
{ {
if (!$this->config['break_reconnect']) {
return false;
}
$info = [ $info = [
'server has gone away', 'server has gone away',
'no connection to the server', 'no connection to the server',