简体   繁体   中英

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;

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:

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();

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. This way was successful. Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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