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 12
12
use Doctrine \DBAL \Connection \StaticServerVersionProvider ;
13
13
use Doctrine \DBAL \Driver \API \ExceptionConverter ;
14
14
use Doctrine \DBAL \Driver \Connection as DriverConnection ;
15
+ use Doctrine \DBAL \Driver \Exception as TheDriverException ;
15
16
use Doctrine \DBAL \Driver \Statement as DriverStatement ;
16
17
use Doctrine \DBAL \Exception \CommitFailedRollbackOnly ;
17
18
use Doctrine \DBAL \Exception \ConnectionLost ;
@@ -939,7 +940,20 @@ public function transactional(Closure $func): mixed
939
940
}
940
941
}
941
942
942
- $ this ->commit ();
943
+ $ shouldRollback = true ;
944
+ try {
945
+ $ this ->commit ();
946
+
947
+ $ shouldRollback = false ;
948
+ } catch (TheDriverException $ t ) {
949
+ $ shouldRollback = false ;
950
+
951
+ throw $ t ;
952
+ } finally {
953
+ if ($ shouldRollback ) {
954
+ $ this ->rollBack ();
955
+ }
956
+ }
943
957
944
958
return $ res ;
945
959
}
Original file line number Diff line number Diff line change @@ -644,4 +644,26 @@ public function rollBack(): void
644
644
self ::assertSame ('Original exception ' , $ e ->getPrevious ()->getMessage ());
645
645
}
646
646
}
647
+
648
+ /**
649
+ * We are not sure if this can happen in real life scenario
650
+ */
651
+ public function testItFailsDuringCommitBeforeTouchingDb (): void
652
+ {
653
+ $ connection = new class (['memory ' => true ], new Driver \SQLite3 \Driver ()) extends Connection {
654
+ public function commit (): void
655
+ {
656
+ throw new \Exception ('Fail before touching the db ' );
657
+ }
658
+
659
+ public function rollBack (): void
660
+ {
661
+ throw new \Exception ('Rollback got triggered ' );
662
+ }
663
+ };
664
+
665
+ $ this ->expectExceptionMessage ('Rollback got triggered ' );
666
+ $ connection ->transactional (static function (): void {
667
+ });
668
+ }
647
669
}
You can’t perform that action at this time.
0 commit comments