简体   繁体   English

Laravel没有提交我的第二笔交易

[英]Laravel does not commit my second transaction

I have some troubles understanding why does not commit both transactions. 我在理解为什么不同时提交两个事务时遇到了一些麻烦。

I have a application that is running MYSQL database with autocommit disabled; 我有一个正在禁用自动提交的运行MYSQL数据库的应用程序;

During one request, I do two transactions, lets say: 在一个请求中,我进行了两次交易,可以说:

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;
    } 

}

In this case, lets say in one request from a user, Controller A call both methods in the same request: 在这种情况下,假设在一个用户的请求中,控制器A在同一个请求中调用了两个方法:

createDownload(1,toto,toto/toto);
----
----
lines of code
----
----
finishedDownload(toto,toto);

It should basically, in the first method, insert the record, and after few lines of code, update the same record. 它基本上应该在第一种方法中插入记录,并在几行代码之后更新同一条记录。

BUT! 但! It insert the record, and commit, but then when suppose to update it, it does not update the record! 它插入记录并提交,但随后假设要更新它,则它不会更新记录! When i check what the query returns after update, it returns 1 confirming that the query went ahead, but it does not do the commit(); 当我检查更新后查询返回的内容时,它返回1确认查询继续进行,但不执行commit();

Can anybody explain me the reason why, and how I can fix this bug? 谁能向我解释原因以及如何解决此错误?

Thank you! 谢谢!

After trying different thing with just transaction() without any luck, I did use beginTransaction() and close them with commit or rollback. 在没有任何运气的情况下,仅使用transaction()尝试了其他事情之后,我确实使用了beginTransaction()并通过commit或rollback关闭它们。 This way was successful. 这样成功了。 Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM