[英]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.