我的 Controller 代码的结构如下:
$connection = ConnectionManager::get('default');
$connection->begin();
try {
//Some logic here to create entities, validate and save them
//This code can throw exceptions
$connection->commit();
}
catch (Exception $e) {
$connection->rollback();
}
这是在 CakePHP 中使用事务的正确方法吗?如果确实抛出异常,事务会正确回滚,但会出现警告:
警告:连接将要关闭,但存在事件事务。
我想,我在某个地方错过了重要的一点,因为我不明白为什么会发出这个警告。我一直认为,commit()
或 rollback()
都会关闭事务。我错了吗?
最佳答案
好的,所以我从两三个月以来一直在寻找这个问题的原因,就在我提出这个问题之后,我找到了它! 大家,这就是 Stackoverflow 的魔法。
我的异常从未被捕获,因为我错过了 catch
指令中的 \
。由于我的代码位于 Controller 中,PHP 假定仅捕获 App\Controller\Exception
(默认 Controller 命名空间)。要捕获所有异常,代码必须是
catch (\Exception $e) {
简单。
关于mysql - 警告 : The connection is going to be closed but there is an active transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408014/