PDO:There is no active transaction

报错

sentry后台看到这个错误There is no active transaction,这个问题出现在一个update方法里,大致是

function update() {
    $this->db->beginTrans();
    try {
        dosomething();
        $this->db->commitTrans();
    } catch (\Exception $ex) {
        this->db->rollBackTrans();
        throw $ex;
    }
}

dosomething()方法我也仔细检查过来,没有开启事务,结束事务的地方,报错位置就在$this->db->commitTrans();这一行,有点奇怪为什么没有事务?

db类

我猜测是db类的问题(没开启事务),这个类是我拿workman mysql helper改的(加了一个主从读取而已),源码在这里,我开启事务部分是

public function beginTrans()
{
    $this->Trans = true;
    try {
        return $this->pdo->beginTransaction();
    } catch (PDOException $e) {
        // 服务端断开时重连一次
        if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
            $this->closeConnection();
            $this->pdoRW = $this->getConnectionInstance($this->configs['rw']);
            $this->pdo = $this->pdoRW;
            return $this->pdo->beginTransaction();
        } else {
            throw $e;
        }
    }
}

不过,也是会开启事务,很奇怪问题发生原因(这个问题有时候会发生)

阅读 7.3k
1 个回答

有可能不是一个session.. 其实没必要重连一次.. 要找连接失败的原因. 而不是重试连接..

推荐问题