Skip to content

Commit 3221612

Browse files
Nandan108claude
andcommitted
Keep the user transaction active after a failed statement
When a statement failed, the driver rolled back the whole user transaction, including the work done before the failed statement, and ended it. MySQL undoes only the failed statement and keeps the transaction active, so the caller can decide whether to commit or roll back the rest. A later COMMIT kept only the work done after the error, and a savepoint created before the error could no longer be used. Roll back only the wrapper transaction of the failed statement. Inside a user transaction, the wrapper is a savepoint, so the transaction stays active. This also keeps an active LOCK TABLES transaction, and replaces the special case for a missing savepoint, since savepoint statements are not wrapped. Part of #473. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6546e45 commit 3221612

3 files changed

Lines changed: 140 additions & 8 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,16 +1193,18 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11931193
$this->error_info = array( '00000', null, null );
11941194
return $stmt;
11951195
} catch ( Throwable $e ) {
1196-
// MySQL error 1305 reports a missing savepoint without ending the transaction.
1197-
$preserves_transaction = $e instanceof WP_MySQL_On_SQLite_Exception
1198-
&& 1305 === ( $e->errorInfo[1] ?? null );
1199-
if ( ! $preserves_transaction ) {
1200-
try {
1201-
$this->rollback_user_transaction();
1196+
/*
1197+
* Undo the failed statement. Like MySQL, keep an active user
1198+
* transaction open, so the caller can decide whether to commit
1199+
* or roll back the rest of it.
1200+
*/
1201+
try {
1202+
$this->rollback_wrapper_transaction();
1203+
if ( ! $this->inTransaction() ) {
12021204
$this->table_lock_active = false;
1203-
} catch ( Throwable $rollback_exception ) {
1204-
// Ignore rollback errors.
12051205
}
1206+
} catch ( Throwable $rollback_exception ) {
1207+
// Ignore rollback errors.
12061208
}
12071209
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
12081210
$e = $this->convert_information_schema_exception( $e );
@@ -2088,6 +2090,47 @@ private function commit_wrapper_transaction(): void {
20882090
$this->in_transaction = $in_transaction;
20892091
}
20902092

2093+
/**
2094+
* Roll back a wrapper transaction.
2095+
*
2096+
* This undoes all changes of a failed statement. When the wrapper is
2097+
* a savepoint, the user transaction around it stays active.
2098+
*/
2099+
private function rollback_wrapper_transaction(): void {
2100+
if ( null === $this->wrapper_transaction_type ) {
2101+
return;
2102+
}
2103+
2104+
try {
2105+
if ( 'savepoint' === $this->wrapper_transaction_type ) {
2106+
$savepoint_name = $this->get_internal_savepoint_name( 'wrapper' );
2107+
$this->connection->query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
2108+
$this->connection->query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
2109+
} else {
2110+
$this->connection->query( 'ROLLBACK' );
2111+
$this->in_transaction = false;
2112+
$this->savepoint_names = array();
2113+
}
2114+
} catch ( PDOException $e ) {
2115+
/*
2116+
* Some SQLite errors roll back the whole transaction automatically,
2117+
* which also removes the savepoint. In that case, or if the rollback
2118+
* fails for any other reason, end the transaction.
2119+
*
2120+
* @see https://www.sqlite.org/lang_transaction.html#response_to_errors_within_a_transaction
2121+
*/
2122+
try {
2123+
$this->connection->query( 'ROLLBACK' );
2124+
} catch ( PDOException $rollback_exception ) {
2125+
// The transaction was already rolled back.
2126+
}
2127+
$this->in_transaction = false;
2128+
$this->savepoint_names = array();
2129+
} finally {
2130+
$this->wrapper_transaction_type = null;
2131+
}
2132+
}
2133+
20912134
/**
20922135
* Execute the "BEGIN" or "START TRANSACTION" MySQL statement in SQLite.
20932136
*/

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,22 @@ public function test_rollback(): void {
888888
$this->assertTrue( $result );
889889
}
890890

891+
public function test_failed_query_keeps_transaction_active(): void {
892+
$this->driver->exec( 'CREATE TABLE t (id INT PRIMARY KEY)' );
893+
$this->driver->beginTransaction();
894+
$this->driver->exec( 'INSERT INTO t VALUES (1)' );
895+
try {
896+
$this->driver->exec( 'INSERT INTO t VALUES (1)' );
897+
$this->fail( 'An exception was expected, but none was thrown.' );
898+
} catch ( PDOException $e ) {
899+
$this->assertSame( '23000', $e->getCode() );
900+
}
901+
902+
$this->assertTrue( $this->driver->inTransaction() );
903+
$this->assertTrue( $this->driver->commit() );
904+
$this->assertSame( array( '1' ), $this->driver->query( 'SELECT id FROM t' )->fetchAll( PDO::FETCH_COLUMN ) );
905+
}
906+
891907
public function test_rollback_no_active_transaction(): void {
892908
$this->expectException( PDOException::class );
893909
$this->expectExceptionMessage( 'There is no active transaction' );

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7920,6 +7920,79 @@ public function testTransactionSavepoints(): void {
79207920
$this->assertSame( array(), (array) array_column( $result, 'id' ) );
79217921
}
79227922

7923+
public function testFailedStatementKeepsTransactionActive(): void {
7924+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
7925+
$this->assertQuery( 'BEGIN' );
7926+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
7927+
7928+
// A failed write and a failed read keep the transaction active.
7929+
$this->assertQueryError(
7930+
'INSERT INTO t VALUES (1)',
7931+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
7932+
);
7933+
$this->assertTrue( $this->engine->inTransaction() );
7934+
$this->assertQueryError(
7935+
'SELECT * FROM missing_table',
7936+
"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'missing_table' doesn't exist"
7937+
);
7938+
$this->assertTrue( $this->engine->inTransaction() );
7939+
7940+
$this->assertQuery( 'INSERT INTO t VALUES (2)' );
7941+
$this->assertQuery( 'COMMIT' );
7942+
$result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
7943+
$this->assertSame( array( '1', '2' ), array_column( $result, 'id' ) );
7944+
}
7945+
7946+
public function testFailedStatementIsRolledBackInTransaction(): void {
7947+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
7948+
$this->assertQuery( 'BEGIN' );
7949+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
7950+
7951+
// The first row of the failed statement is not kept.
7952+
$this->assertQueryError(
7953+
'INSERT INTO t VALUES (2), (1)',
7954+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
7955+
);
7956+
7957+
$this->assertQuery( 'COMMIT' );
7958+
$result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
7959+
$this->assertSame( array( '1' ), array_column( $result, 'id' ) );
7960+
}
7961+
7962+
public function testRollbackToSavepointAfterFailedStatement(): void {
7963+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
7964+
$this->assertQuery( 'BEGIN' );
7965+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
7966+
$this->assertQuery( 'SAVEPOINT sp1' );
7967+
$this->assertQuery( 'INSERT INTO t VALUES (2)' );
7968+
$this->assertQueryError(
7969+
'INSERT INTO t VALUES (1)',
7970+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
7971+
);
7972+
7973+
// The savepoint still exists after the failed statement.
7974+
$this->assertQuery( 'ROLLBACK TO SAVEPOINT sp1' );
7975+
$this->assertQuery( 'COMMIT' );
7976+
$result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
7977+
$this->assertSame( array( '1' ), array_column( $result, 'id' ) );
7978+
}
7979+
7980+
public function testFailedStatementKeepsTableLock(): void {
7981+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
7982+
$this->assertQuery( 'LOCK TABLES t WRITE' );
7983+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
7984+
$this->assertQueryError(
7985+
'INSERT INTO t VALUES (1)',
7986+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
7987+
);
7988+
7989+
// UNLOCK TABLES keeps the rows written before the failed statement.
7990+
$this->assertQuery( 'UNLOCK TABLES' );
7991+
$this->assertFalse( $this->engine->inTransaction() );
7992+
$result = $this->assertQuery( 'SELECT id FROM t' );
7993+
$this->assertSame( array( '1' ), array_column( $result, 'id' ) );
7994+
}
7995+
79237996
public function testSavepointWithoutTransactionDoesNotStartTransaction(): void {
79247997
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, v INT)' );
79257998
$this->assertQuery( 'INSERT INTO t (id, v) VALUES (1, 1)' );

0 commit comments

Comments
 (0)