[英]Laravel does not commit my second transaction
我在理解为什么不同时提交两个事务时遇到了一些麻烦。
我有一个正在禁用自动提交的运行MYSQL数据库的应用程序;
在一个请求中,我进行了两次交易,可以说:
public function createDownload($user_id,$filename,$path){
$response = null;
try {
DB::transaction(function () use ($filename,$path,$user_id) {
$output = DB::table($this->table)->insert(array('file_name' => $filename, 'path' => $path,'user_id'=>$user_id));
$this->response = $output;
});
DB::commit();
DB::disconnect('mysql');
return $this->response;
} catch (Exception $e) {
DB::rollback();
Log::error($e->getMessage());
return False;
}
}
public function finishedDownload($filename,$path){
DB::reconnect('mysql');
$response = null;
try {
DB::transaction(function () use ($path) {
$output = DB::table($this->table)->where('path', '=', $path)->update(array('status'=>1));
$this->response = $output;
});
Log::info($this->response);
DB::commit();
return $this->response;
} catch (Exception $e) {
DB::rollback();
Log::error($e->getMessage());
return False;
}
}
在这种情况下,假设在一个用户的请求中,控制器A在同一个请求中调用了两个方法:
createDownload(1,toto,toto/toto);
----
----
lines of code
----
----
finishedDownload(toto,toto);
它基本上应该在第一种方法中插入记录,并在几行代码之后更新同一条记录。
但! 它插入记录并提交,但随后假设要更新它,则它不会更新记录! 当我检查更新后查询返回的内容时,它返回1确认查询继续进行,但不执行commit();
谁能向我解释原因以及如何解决此错误?
谢谢!
在没有任何运气的情况下,仅使用transaction()尝试了其他事情之后,我确实使用了beginTransaction()并通过commit或rollback关闭它们。 这样成功了。 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.