Skip to content

Commit 4ae6d34

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 b5dc0cc commit 4ae6d34

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
@@ -1210,16 +1210,18 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
12101210
$this->error_info = array( '00000', null, null );
12111211
return $stmt;
12121212
} catch ( Throwable $e ) {
1213-
// MySQL error 1305 reports a missing savepoint without ending the transaction.
1214-
$preserves_transaction = $e instanceof WP_MySQL_On_SQLite_Exception
1215-
&& 1305 === ( $e->errorInfo[1] ?? null );
1216-
if ( ! $preserves_transaction ) {
1217-
try {
1218-
$this->rollback_user_transaction();
1213+
/*
1214+
* Undo the failed statement. Like MySQL, keep an active user
1215+
* transaction open, so the caller can decide whether to commit
1216+
* or roll back the rest of it.
1217+
*/
1218+
try {
1219+
$this->rollback_wrapper_transaction();
1220+
if ( ! $this->inTransaction() ) {
12191221
$this->table_lock_active = false;
1220-
} catch ( Throwable $rollback_exception ) {
1221-
// Ignore rollback errors.
12221222
}
1223+
} catch ( Throwable $rollback_exception ) {
1224+
// Ignore rollback errors.
12231225
}
12241226
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
12251227
$e = $this->convert_information_schema_exception( $e );
@@ -2105,6 +2107,47 @@ private function commit_wrapper_transaction(): void {
21052107
$this->in_transaction = $in_transaction;
21062108
}
21072109

2110+
/**
2111+
* Roll back a wrapper transaction.
2112+
*
2113+
* This undoes all changes of a failed statement. When the wrapper is
2114+
* a savepoint, the user transaction around it stays active.
2115+
*/
2116+
private function rollback_wrapper_transaction(): void {
2117+
if ( null === $this->wrapper_transaction_type ) {
2118+
return;
2119+
}
2120+
2121+
try {
2122+
if ( 'savepoint' === $this->wrapper_transaction_type ) {
2123+
$savepoint_name = $this->get_internal_savepoint_name( 'wrapper' );
2124+
$this->connection->query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
2125+
$this->connection->query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
2126+
} else {
2127+
$this->connection->query( 'ROLLBACK' );
2128+
$this->in_transaction = false;
2129+
$this->savepoint_names = array();
2130+
}
2131+
} catch ( PDOException $e ) {
2132+
/*
2133+
* Some SQLite errors roll back the whole transaction automatically,
2134+
* which also removes the savepoint. In that case, or if the rollback
2135+
* fails for any other reason, end the transaction.
2136+
*
2137+
* @see https://www.sqlite.org/lang_transaction.html#response_to_errors_within_a_transaction
2138+
*/
2139+
try {
2140+
$this->connection->query( 'ROLLBACK' );
2141+
} catch ( PDOException $rollback_exception ) {
2142+
// The transaction was already rolled back.
2143+
}
2144+
$this->in_transaction = false;
2145+
$this->savepoint_names = array();
2146+
} finally {
2147+
$this->wrapper_transaction_type = null;
2148+
}
2149+
}
2150+
21082151
/**
21092152
* Execute the "BEGIN" or "START TRANSACTION" MySQL statement in SQLite.
21102153
*/

‎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
@@ -1003,6 +1003,22 @@ public function test_rollback_method_as_first_transaction_on_connection(): void
10031003
$this->assertSame( 0, $this->driver->exec( 'CREATE TABLE t (id INT)' ) );
10041004
}
10051005

1006+
public function test_failed_query_keeps_transaction_active(): void {
1007+
$this->driver->exec( 'CREATE TABLE t (id INT PRIMARY KEY)' );
1008+
$this->driver->beginTransaction();
1009+
$this->driver->exec( 'INSERT INTO t VALUES (1)' );
1010+
try {
1011+
$this->driver->exec( 'INSERT INTO t VALUES (1)' );
1012+
$this->fail( 'An exception was expected, but none was thrown.' );
1013+
} catch ( PDOException $e ) {
1014+
$this->assertSame( '23000', $e->getCode() );
1015+
}
1016+
1017+
$this->assertTrue( $this->driver->inTransaction() );
1018+
$this->assertTrue( $this->driver->commit() );
1019+
$this->assertSame( array( '1' ), $this->driver->query( 'SELECT id FROM t' )->fetchAll( PDO::FETCH_COLUMN ) );
1020+
}
1021+
10061022
public function test_rollback_no_active_transaction(): void {
10071023
$this->expectException( PDOException::class );
10081024
$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
@@ -8335,6 +8335,79 @@ public function testTransactionSavepoints(): void {
83358335
$this->assertSame( array(), (array) array_column( $result, 'id' ) );
83368336
}
83378337

8338+
public function testFailedStatementKeepsTransactionActive(): void {
8339+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
8340+
$this->assertQuery( 'BEGIN' );
8341+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
8342+
8343+
// A failed write and a failed read keep the transaction active.
8344+
$this->assertQueryError(
8345+
'INSERT INTO t VALUES (1)',
8346+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
8347+
);
8348+
$this->assertTrue( $this->engine->inTransaction() );
8349+
$this->assertQueryError(
8350+
'SELECT * FROM missing_table',
8351+
"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'missing_table' doesn't exist"
8352+
);
8353+
$this->assertTrue( $this->engine->inTransaction() );
8354+
8355+
$this->assertQuery( 'INSERT INTO t VALUES (2)' );
8356+
$this->assertQuery( 'COMMIT' );
8357+
$result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
8358+
$this->assertSame( array( '1', '2' ), array_column( $result, 'id' ) );
8359+
}
8360+
8361+
public function testFailedStatementIsRolledBackInTransaction(): void {
8362+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
8363+
$this->assertQuery( 'BEGIN' );
8364+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
8365+
8366+
// The first row of the failed statement is not kept.
8367+
$this->assertQueryError(
8368+
'INSERT INTO t VALUES (2), (1)',
8369+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
8370+
);
8371+
8372+
$this->assertQuery( 'COMMIT' );
8373+
$result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
8374+
$this->assertSame( array( '1' ), array_column( $result, 'id' ) );
8375+
}
8376+
8377+
public function testRollbackToSavepointAfterFailedStatement(): void {
8378+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
8379+
$this->assertQuery( 'BEGIN' );
8380+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
8381+
$this->assertQuery( 'SAVEPOINT sp1' );
8382+
$this->assertQuery( 'INSERT INTO t VALUES (2)' );
8383+
$this->assertQueryError(
8384+
'INSERT INTO t VALUES (1)',
8385+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
8386+
);
8387+
8388+
// The savepoint still exists after the failed statement.
8389+
$this->assertQuery( 'ROLLBACK TO SAVEPOINT sp1' );
8390+
$this->assertQuery( 'COMMIT' );
8391+
$result = $this->assertQuery( 'SELECT id FROM t ORDER BY id' );
8392+
$this->assertSame( array( '1' ), array_column( $result, 'id' ) );
8393+
}
8394+
8395+
public function testFailedStatementKeepsTableLock(): void {
8396+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY)' );
8397+
$this->assertQuery( 'LOCK TABLES t WRITE' );
8398+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
8399+
$this->assertQueryError(
8400+
'INSERT INTO t VALUES (1)',
8401+
'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.id'
8402+
);
8403+
8404+
// UNLOCK TABLES keeps the rows written before the failed statement.
8405+
$this->assertQuery( 'UNLOCK TABLES' );
8406+
$this->assertFalse( $this->engine->inTransaction() );
8407+
$result = $this->assertQuery( 'SELECT id FROM t' );
8408+
$this->assertSame( array( '1' ), array_column( $result, 'id' ) );
8409+
}
8410+
83388411
public function testSavepointWithoutTransactionDoesNotStartTransaction(): void {
83398412
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, v INT)' );
83408413
$this->assertQuery( 'INSERT INTO t (id, v) VALUES (1, 1)' );

0 commit comments

Comments
 (0)