File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 9
9
use Doctrine \DBAL \Cache \QueryCacheProfile ;
10
10
use Doctrine \DBAL \Driver \API \ExceptionConverter ;
11
11
use Doctrine \DBAL \Driver \Connection as DriverConnection ;
12
+ use Doctrine \DBAL \Driver \Exception as TheDriverException ;
12
13
use Doctrine \DBAL \Driver \ServerInfoAwareConnection ;
13
14
use Doctrine \DBAL \Driver \Statement as DriverStatement ;
14
15
use Doctrine \DBAL \Event \TransactionBeginEventArgs ;
@@ -1291,7 +1292,20 @@ public function transactional(Closure $func)
1291
1292
}
1292
1293
}
1293
1294
1294
- $ this ->commit ();
1295
+ $ shouldRollback = true ;
1296
+ try {
1297
+ $ this ->commit ();
1298
+
1299
+ $ shouldRollback = false ;
1300
+ } catch (TheDriverException $ t ) {
1301
+ $ shouldRollback = false ;
1302
+
1303
+ throw $ t ;
1304
+ } finally {
1305
+ if ($ shouldRollback ) {
1306
+ $ this ->rollBack ();
1307
+ }
1308
+ }
1295
1309
1296
1310
return $ res ;
1297
1311
}
Original file line number Diff line number Diff line change @@ -1025,6 +1025,28 @@ public function rollBack(): void
1025
1025
self ::assertSame ('Original exception ' , $ e ->getPrevious ()->getMessage ());
1026
1026
}
1027
1027
}
1028
+
1029
+ /**
1030
+ * We are not sure if this can happen in real life scenario
1031
+ */
1032
+ public function testItFailsDuringCommitBeforeTouchingDb (): void
1033
+ {
1034
+ $ connection = new class (['memory ' => true ], new Driver \SQLite3 \Driver ()) extends Connection {
1035
+ public function commit (): void
1036
+ {
1037
+ throw new Exception ('Fail before touching the db ' );
1038
+ }
1039
+
1040
+ public function rollBack (): void
1041
+ {
1042
+ throw new Exception ('Rollback got triggered ' );
1043
+ }
1044
+ };
1045
+
1046
+ $ this ->expectExceptionMessage ('Rollback got triggered ' );
1047
+ $ connection ->transactional (static function (): void {
1048
+ });
1049
+ }
1028
1050
}
1029
1051
1030
1052
interface ConnectDispatchEventListener
You can’t perform that action at this time.
0 commit comments