getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
632 stars 94 forks source link

Transactions not working #125

Open DiogoBatistaComponto opened 8 years ago

DiogoBatistaComponto commented 8 years ago

Hello, i'm having an issue with transactions, i need to execute some table operations and want to be sure that every update is successful before continue my actions. Since you do not have explicit documentation on Transactions, im using the example below:

composer.json:

{
 "name": "getherbert/herbert-plugin",
 "description": "Herbert plugin",
 "license": "MIT",
 "require": {
   "getherbert/framework": "~0.9",
   "phpmailer/phpmailer": "^5.2"
 },
 "config": {
   "preferred-install": "dist"
 },
 "minimum-stability": "dev",
 "autoload": {
   "psr-4": {
     "LatinaStreaming\\": "app/"
   }
 }
}

Sample code i'm using:

use Illuminate\Database\Capsule\Manager as Capsule;

try {
Capsule::beginTransaction();

Capsule::table('streaming_configuration_table')
->where('id', '1')
->update(
[
'current_table' => 'qwerty',
]
);
Capsule::rollback();
} catch (\Exception $e) {

var_dump($e);

}

The expected result is that the update will have no effect but it seems that, somehow, the update commits successfully, not being able to rollback.

Can you please help me with this situation?

Thank you, Waiting for your feedback

arippberger commented 8 years ago

@DiogoBatistaComponto could you try structuring your code like this?


try {

   Capsule::beginTransaction();

   Capsule::table('streaming_configuration_table')
      ->where('id', '1')
      ->update(
         [
            'current_table' => 'qwerty',
         ]
   );

   Capsule::commit();

} catch (\Exception $e){
    Capsule::rollback();
    var_dump($e);
}
arippberger commented 8 years ago

https://laravel.com/docs/5.2/database#database-transactions

DiogoBatistaComponto commented 8 years ago

@arippberger Thanks for the reply but i already tried that code and it does work, i can successfully see the update in the database. but what i want is to force the rollback and see the transaction failing so i can be sure that all operations in that transactions only happen if non of them fails. And this i can not do.

Every time i force the rollback, or force a Exception, rollback doesn't work.

arippberger commented 8 years ago
 try {

   Capsule::beginTransaction();

   Capsule::table('streaming_configuration_table')
      ->where('id', '1')
      ->update(
         [
            'current_table' => 'qwerty',
         ]
   );

   Capsule::table('streaming_configuration_table')
      ->where('id', 'SOME_NUMBER_NOT_IN_DB')
      ->update(
         [
            'current_table' => 'qwerty',
         ]
   );

   Capsule::commit();

} catch (\Exception $e){
    Capsule::rollback();
    var_dump($e);
} 

@DiogoBatistaComponto Can you confirm that this does not update current_table for me?

DiogoBatistaComponto commented 8 years ago

Hello @arippberger thank you for your reply, i tried your solution and the outcome was that the first set of code worked and updated the table and the second one didn't sort any kind of result, it didn't insert any record and didn't rollback the update that was done in the first set of code. I think that you wanted to force a SQL exception on the second update and do the rollback but it didn't worked.

Thank you

DiogoBatistaComponto commented 8 years ago

Hello,

Didn't get a response to this issue, do you have any solutions??

Thank you