File tree 2 files changed +30
-4
lines changed
2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -926,15 +926,20 @@ public function lastInsertId(): int|string
926
926
public function transactional (Closure $ func ): mixed
927
927
{
928
928
$ this ->beginTransaction ();
929
+
930
+ $ successful = false ;
931
+
929
932
try {
930
933
$ res = $ func ($ this );
931
934
$ this ->commit ();
932
935
933
- return $ res ;
934
- } catch (Throwable $ e ) {
935
- $ this ->rollBack ();
936
+ $ successful = true ;
936
937
937
- throw $ e ;
938
+ return $ res ;
939
+ } finally {
940
+ if (! $ successful ) {
941
+ $ this ->rollBack ();
942
+ }
938
943
}
939
944
}
940
945
Original file line number Diff line number Diff line change @@ -623,4 +623,25 @@ public function testDefaultSchemaManagerFactory(): void
623
623
$ connection = DriverManager::getConnection (['driver ' => 'sqlite3 ' , 'memory ' => true ]);
624
624
self ::assertInstanceOf (SQLiteSchemaManager::class, $ connection ->createSchemaManager ());
625
625
}
626
+
627
+ public function testItPreservesTheOriginalExceptionOnRollbackFailure (): void
628
+ {
629
+ $ connection = new class (['memory ' => true ], new Driver \SQLite3 \Driver ()) extends Connection {
630
+ public function rollBack (): void
631
+ {
632
+ throw new ConnectionException ('Rollback exception ' );
633
+ }
634
+ };
635
+
636
+ try {
637
+ $ connection ->transactional (static function (): void {
638
+ throw new ConnectionException ('Original exception ' );
639
+ });
640
+ self ::fail ('Exception expected ' ); // @phpstan-ignore deadCode.unreachable
641
+ } catch (ConnectionException $ e ) {
642
+ self ::assertSame ('Rollback exception ' , $ e ->getMessage ());
643
+ self ::assertNotNull ($ e ->getPrevious ());
644
+ self ::assertSame ('Original exception ' , $ e ->getPrevious ()->getMessage ());
645
+ }
646
+ }
626
647
}
You can’t perform that action at this time.
0 commit comments