File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3131class Postgres extends SQL
3232{
3333 public const MAX_IDENTIFIER_NAME = 63 ;
34- /**
35- * @inheritDoc
36- */
37- public function startTransaction (): bool
38- {
39- try {
40- if ($ this ->inTransaction === 0 ) {
41- try {
42- if ($ this ->getPDO ()->inTransaction ()) {
43- $ this ->getPDO ()->rollBack ();
44- } else {
45- // If no active transaction, this has no effect.
46- $ this ->getPDO ()->prepare ('ROLLBACK ' )->execute ();
47- }
48- } catch (PDOException ) {
49- // A pooled connection can report a transaction it no longer
50- // holds after a reconnect (e.g. Swoole PDOProxy keeps its own
51- // counter), making this cleanup rollback throw. It is best
52- // effort; swallow it and begin a fresh transaction below.
53- }
54-
55- $ result = $ this ->getPDO ()->beginTransaction ();
56- } else {
57- $ this ->getPDO ()->exec ('SAVEPOINT transaction ' . $ this ->inTransaction );
58- $ result = true ;
59- }
60- } catch (PDOException $ e ) {
61- throw new TransactionException ('Failed to start transaction: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
62- }
63-
64- if (!$ result ) {
65- throw new TransactionException ('Failed to start transaction ' );
66- }
67-
68- $ this ->inTransaction ++;
69-
70- return $ result ;
71- }
7234
7335 /**
7436 * @inheritDoc
Original file line number Diff line number Diff line change 44
55use PDOException ;
66use PHPUnit \Framework \TestCase ;
7+ use ReflectionMethod ;
78use Utopia \Database \Adapter \MySQL ;
89use Utopia \Database \Adapter \Postgres ;
10+ use Utopia \Database \Adapter \SQL ;
911use Utopia \Database \Exception \Transaction as TransactionException ;
1012
11- class SQLTransactionTest extends TestCase
13+ final class SQLTransactionTest extends TestCase
1214{
1315 /**
1416 * A pooled connection (e.g. Swoole PDOProxy) can keep its own transaction
@@ -61,6 +63,9 @@ public function testStartTransactionDoesNotMaskBeginFailureAfterDesyncedRollback
6163
6264 public function testPostgresStartTransactionRecoversFromDesyncedRollback (): void
6365 {
66+ $ method = new ReflectionMethod (Postgres::class, 'startTransaction ' );
67+ $ this ->assertSame (SQL ::class, $ method ->getDeclaringClass ()->getName ());
68+
6469 $ pdo = $ this ->getMockBuilder (\PDO ::class)
6570 ->disableOriginalConstructor ()
6671 ->getMock ();
You can’t perform that action at this time.
0 commit comments