Php – Laravel DB::transaction not rolling back on exception

databaselaravelMySQLPHP

I'm having this small issue with Laravel 4.2 and DB::transaction. I was having an issue with transactions not being rolled back so I tried the easiest snippet of code and put it into routes.php for testing purposes:

routes.php:

DB::transaction(function(){

  $user = App::make("User");
  $user->save();
  throw new Exception("Should not create users");
});
...
...
... 
Some other code here 

Simply I try to create user within transaction closure and after user is created I throw exception to force rollback of transaction. My problem is that even thou exception is thrown, the transaction does not roll back. I'm getting new user in the database every time I refresh the app. The same code works as intended on the local machine, but on the server i'm planning to use for production it simply does not roll the transaction back. Do you have any ideas why ?

EDIT:

Server MySql: 5.1.73-cll – MySQL Community Server (GPLv2)

Server PHP: PHP 5.4.30 (cli) (built: Jul 19 2014 15:22:18)

Local PHP: 5.5.9

Local MySql: 5.6.16

Server is sitting on CentOs while local machine is Windows 7.

Best Answer

So I'm responding to my own question. InnoDb was not a default storage engine until MySql 5.5. In my case MYISAM was the default storage engine and did not support the transactions. What I had to do is enable InnoDB in my CPanel server installation of MySQL. Than I had to make sure each of the tables in my Laravel migrations was created with InnoDB engine. I did that by adding:

     $table->engine = "InnoDB"; 

to each migration file. Once all the tables were set up with InnoDB engine, transactions work as intended.

Related Topic