Laravel - transaction rollback not working

Mamen

i have code for saving user and code for sending mail, but i want to rollback if there is an error with saving or sending mail, this is my code looks like :

\DB::beginTransaction();

try{
    $user->save();
    //role
    $user->attachRole($role);
    //send email

    \DB::commit();
    Mail::to($user->email)->send(new ActivationCode($user));
    return response()->json([
        'status' => 'success',
        'message' => 'Berhasil mendaftar, cek email anda untuk mendapatkan kode aktivasi'
    ], 201);    

}catch(\Exception $e){
    \DB::rollback();
    return response()->json([
        'status' => 'fail',
        'message' => 'Terjadi kesalahan, harap periksa kembali form pendaftaran'
    ], 400);  
}   

the mail code was intentionally made an error.

the problem: the catch is reached but the data still inserted into the database

Davit

You must be firstly send mail, then commit changes.

\DB::beginTransaction();

try{
    $user->save();
    //role
    $user->attachRole($role);
    //send email

    Mail::to($user->email)->send(new ActivationCode($user));
    \DB::commit();
    return response()->json([
        'status' => 'success',
        'message' => 'Berhasil mendaftar, cek email anda untuk mendapatkan kode aktivasi'
    ], 201);    

}catch(\Exception $e){
    \DB::rollback();
    return response()->json([
        'status' => 'fail',
        'message' => 'Terjadi kesalahan, harap periksa kembali form pendaftaran'
    ], 400);  
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rollback is working on a transaction but crashes anyway by SQLNonTransientConnectionException

Laravel Transaction: How to do commit some queries instead of rollback all?

Transaction rollback in a reactive application

Rollback transaction after @Test

Transaction rollback and web services

Hibernate save() and transaction rollback

Managing rollback on transaction

JTA container managed transaction rollback not working for jaxrs endpoint

JDBC Informix Transaction Rollback

MyBatis-Spring rollback not working with multiple transaction managers

Rollback a committed transaction

Conditional transaction rollback

Rollback transaction in hibernate

Transaction rollback in innodb mysql/php with mysql_query not working

JMS Transaction Rollback Property not working in WSO2 4.8.1 ESB

Rollback Transaction not working in SSIS

Intercept EJB transaction rollback

Transaction: commit() vs rollBack()

HibernateTransactionManager rollback transaction

MySQL InnoDB transaction rollback is not working

Spring rollback transaction part

Rails rollback transaction for bids

Spring's TestNG rollback transaction not working

laravel, mysql transaction not working once rollback

tSQLt.ExceptException is not working with rollback transaction in SQL Server

MySQL Transaction doesn't rollback with 0 affected rows - Laravel

Rollback of Prisma Interactive Transaction in NestJS not working when throwing an error

Seqelize transaction rollback is not working

Transaction rollback working yet an error is still getting generated