我很难理解为什么不提交这两个事务。
我有一个正在运行禁用自动提交的 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;
}
}
在这种情况下,假设在用户的一个请求中, Controller A 在同一请求中调用这两个方法:
createDownload(1,toto,toto/toto);
----
----
lines of code
----
----
finishedDownload(toto,toto);
基本上应该在第一个方法中插入记录,然后在几行代码之后更新相同的记录。
但是!它插入记录并提交,但是当假设更新它时,它不会更新记录! 当我检查更新后查询返回的内容时,它返回 1 确认查询继续进行,但它不执行 commit();谁能解释一下原因,以及如何修复这个错误?
谢谢!
最佳答案
在仅使用 transaction() 尝试不同的事情但没有任何运气之后,我确实使用了 beginTransaction() 并通过提交或回滚关闭它们。 这个办法成功了。 谢谢!
关于php - Laravel 没有提交我的第二笔交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33677193/