File tree 2 files changed +34
-9
lines changed
2 files changed +34
-9
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 ;
@@ -1279,12 +1280,29 @@ public function transactional(Closure $func)
1279
1280
{
1280
1281
$ this ->beginTransaction ();
1281
1282
1283
+ $ successful = false ;
1284
+
1282
1285
try {
1283
1286
$ res = $ func ($ this );
1284
1287
1288
+ $ successful = true ;
1289
+ } finally {
1290
+ if (! $ successful ) {
1291
+ $ this ->rollBack ();
1292
+ }
1293
+ }
1294
+
1295
+ $ shouldRollback = true ;
1296
+ try {
1285
1297
$ this ->commit ();
1298
+
1299
+ $ shouldRollback = false ;
1300
+ } catch (TheDriverException $ t ) {
1301
+ $ shouldRollback = false ;
1302
+
1303
+ throw $ t ;
1286
1304
} finally {
1287
- if ($ this -> isTransactionActive () ) {
1305
+ if ($ shouldRollback ) {
1288
1306
$ this ->rollBack ();
1289
1307
}
1290
1308
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Doctrine \DBAL \Tests \Functional ;
4
4
5
+ use Doctrine \DBAL \Connection ;
5
6
use Doctrine \DBAL \Driver \Exception as DriverException ;
6
7
use Doctrine \DBAL \Platforms \AbstractMySQLPlatform ;
7
8
use Doctrine \DBAL \Tests \FunctionalTestCase ;
11
12
12
13
class TransactionTest extends FunctionalTestCase
13
14
{
14
- protected function setUp (): void
15
+ public function testCommitFalse (): void
15
16
{
16
- if ($ this ->connection ->getDatabasePlatform () instanceof AbstractMySQLPlatform) {
17
- return ;
17
+ if (! $ this ->connection ->getDatabasePlatform () instanceof AbstractMySQLPlatform) {
18
+ $ this -> markTestSkipped ( ' Restricted to MySQL. ' ) ;
18
19
}
19
20
20
- $ this ->markTestSkipped ('Restricted to MySQL. ' );
21
- }
22
-
23
- public function testCommitFalse (): void
24
- {
25
21
$ this ->connection ->executeStatement ('SET SESSION wait_timeout=1 ' );
26
22
27
23
self ::assertTrue ($ this ->connection ->beginTransaction ());
@@ -40,4 +36,15 @@ public function testCommitFalse(): void
40
36
$ this ->connection ->close ();
41
37
}
42
38
}
39
+
40
+ public function testNestedTransactionWalkthrough (): void
41
+ {
42
+ $ result = $ this ->connection ->transactional (
43
+ static fn (Connection $ connection ) => $ connection ->transactional (
44
+ static fn (Connection $ connection ) => $ connection ->fetchOne ('SELECT 1 ' ),
45
+ ),
46
+ );
47
+
48
+ self ::assertSame ('1 ' , (string ) $ result );
49
+ }
43
50
}
You can’t perform that action at this time.
0 commit comments