File tree Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -1278,15 +1278,20 @@ public function lastInsertId($name = null)
1278
1278
public function transactional (Closure $ func )
1279
1279
{
1280
1280
$ this ->beginTransaction ();
1281
+
1282
+ $ successful = false ;
1283
+
1281
1284
try {
1282
1285
$ res = $ func ($ this );
1283
1286
$ this ->commit ();
1284
1287
1285
- return $ res ;
1286
- } catch (Throwable $ e ) {
1287
- $ this ->rollBack ();
1288
+ $ successful = true ;
1288
1289
1289
- throw $ e ;
1290
+ return $ res ;
1291
+ } finally {
1292
+ if (! $ successful ) {
1293
+ $ this ->rollBack ();
1294
+ }
1290
1295
}
1291
1296
}
1292
1297
Original file line number Diff line number Diff line change @@ -1004,6 +1004,27 @@ public function testLegacySchemaManagerFactory(): void
1004
1004
$ connection = DriverManager::getConnection (['driver ' => 'sqlite3 ' , 'memory ' => true ]);
1005
1005
self ::assertInstanceOf (SqliteSchemaManager::class, $ connection ->createSchemaManager ());
1006
1006
}
1007
+
1008
+ public function testItPreservesTheOriginalExceptionOnRollbackFailure (): void
1009
+ {
1010
+ $ connection = new class (['memory ' => true ], new Driver \SQLite3 \Driver ()) extends Connection {
1011
+ public function rollBack (): void
1012
+ {
1013
+ throw new Exception ('Rollback exception ' );
1014
+ }
1015
+ };
1016
+
1017
+ try {
1018
+ $ connection ->transactional (static function (): void {
1019
+ throw new Exception ('Original exception ' );
1020
+ });
1021
+ self ::fail ('Exception expected ' );
1022
+ } catch (Exception $ e ) {
1023
+ self ::assertSame ('Rollback exception ' , $ e ->getMessage ());
1024
+ self ::assertNotNull ($ e ->getPrevious ());
1025
+ self ::assertSame ('Original exception ' , $ e ->getPrevious ()->getMessage ());
1026
+ }
1027
+ }
1007
1028
}
1008
1029
1009
1030
interface ConnectDispatchEventListener
You can’t perform that action at this time.
0 commit comments