Skip to content

Commit 0e21f38

Browse files
committed
Rename the busy timeout option and use milliseconds
Rename the sqlite_timeout driver option to sqlite_busy_timeout and the connection timeout option to busy_timeout. Both now take milliseconds, and the connection applies them with PRAGMA busy_timeout. SQLITE_BUSY_TIMEOUT uses milliseconds as well, and DEFAULT_SQLITE_TIMEOUT becomes DEFAULT_SQLITE_BUSY_TIMEOUT (10000). Driver options backed by an SQLite PRAGMA are named after it and take its values, like sqlite_journal_mode and sqlite_synchronous. PDO::ATTR_TIMEOUT only accepts whole seconds, and it overflows to no wait at all for values of 2147484 or more.
1 parent 4d66fa2 commit 0e21f38

7 files changed

Lines changed: 25 additions & 28 deletions

File tree

‎packages/mysql-on-sqlite/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ version and configuring the SQLite connection:
7676
| --- | --- | --- |
7777
| `mysql_version` | MySQL version to emulate, represented as an integer | `80038` |
7878
| `sqlite_pdo` | Existing PDO SQLite connection | A new connection for `path` |
79-
| `sqlite_timeout` | SQLite busy timeout in whole seconds | `10` |
79+
| `sqlite_busy_timeout` | SQLite busy timeout in milliseconds | `10000` |
8080
| `sqlite_journal_mode` | SQLite journal mode | `WAL` |
8181
| `sqlite_synchronous` | SQLite synchronous setting | `NORMAL` in WAL mode; otherwise the SQLite default |
8282

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ class WP_MySQL_On_SQLite extends PDO {
795795
*
796796
* @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
797797
* @type PDO|null $sqlite_pdo Optional. Existing PDO SQLite connection.
798-
* @type int|null $sqlite_timeout Optional. SQLite busy timeout in whole seconds.
799-
* Default null (10 seconds).
798+
* @type int|null $sqlite_busy_timeout Optional. SQLite busy timeout in milliseconds.
799+
* Default 10000.
800800
* @type string|null $sqlite_journal_mode Optional. SQLite journal mode. Default 'WAL'.
801801
* @type string|int|null $sqlite_synchronous Optional. SQLite synchronous setting.
802802
* }
@@ -878,7 +878,7 @@ function ( $key ) {
878878
}
879879

880880
$connection_options = array(
881-
'timeout' => $options['sqlite_timeout'] ?? null,
881+
'busy_timeout' => $options['sqlite_busy_timeout'] ?? null,
882882
'journal_mode' => $options['sqlite_journal_mode'] ?? null,
883883
'synchronous' => $options['sqlite_synchronous'] ?? null,
884884
'pdo_options' => $connection_pdo_options,

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818
class WP_SQLite_Connection {
1919
/**
20-
* The default timeout in seconds for SQLite to wait for a writable lock.
20+
* The default time in milliseconds for SQLite to wait for a lock.
2121
*/
22-
const DEFAULT_SQLITE_TIMEOUT = 10;
22+
const DEFAULT_SQLITE_BUSY_TIMEOUT = 10000;
2323

2424
/**
2525
* The supported SQLite journal modes.
@@ -76,8 +76,8 @@ class WP_SQLite_Connection {
7676
* Must be set when PDO instance is not provided.
7777
* @type PDO|null $pdo Optional. PDO instance with SQLite connection.
7878
* If not provided, a new PDO instance will be created.
79-
* @type int|null $timeout Optional. SQLite timeout in seconds.
80-
* The time to wait for a writable lock.
79+
* @type int|null $busy_timeout Optional. SQLite busy timeout in milliseconds.
80+
* The time to wait for a lock. Defaults to 10000.
8181
* @type string|null $journal_mode Optional. SQLite journal mode. Defaults to WAL.
8282
* @type string|int|null $synchronous Optional. SQLite synchronous setting. Defaults to
8383
* NORMAL when the effective journal mode is WAL.
@@ -107,13 +107,13 @@ public function __construct( array $options ) {
107107
// Throw exceptions on error.
108108
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
109109

110-
// Configure SQLite timeout.
111-
if ( isset( $options['timeout'] ) && is_int( $options['timeout'] ) ) {
112-
$timeout = $options['timeout'];
110+
// Configure SQLite busy timeout.
111+
if ( isset( $options['busy_timeout'] ) && is_int( $options['busy_timeout'] ) ) {
112+
$busy_timeout = $options['busy_timeout'];
113113
} else {
114-
$timeout = self::DEFAULT_SQLITE_TIMEOUT;
114+
$busy_timeout = self::DEFAULT_SQLITE_BUSY_TIMEOUT;
115115
}
116-
$this->pdo->setAttribute( PDO::ATTR_TIMEOUT, $timeout );
116+
$this->query( 'PRAGMA busy_timeout = ' . $busy_timeout );
117117

118118
// Configure SQLite journal mode. Default to WAL for best throughput.
119119
$effective_journal_mode = null;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void
133133
// Connection B with zero timeout — any lock conflict fails immediately.
134134
$conn_b = new WP_SQLite_Connection(
135135
array(
136-
'path' => $this->db_path,
137-
'timeout' => 0,
136+
'path' => $this->db_path,
137+
'busy_timeout' => 0,
138138
)
139139
);
140140
$driver_b = $this->create_driver( $conn_b );
@@ -163,8 +163,8 @@ private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): voi
163163
// Connection B with zero timeout — any lock conflict fails immediately.
164164
$conn_b = new WP_SQLite_Connection(
165165
array(
166-
'path' => $this->db_path,
167-
'timeout' => 0,
166+
'path' => $this->db_path,
167+
'busy_timeout' => 0,
168168
)
169169
);
170170
$driver_b = $this->create_driver( $conn_b );
@@ -195,7 +195,7 @@ public function testWriteQueryFailsImmediatelyWithZeroBusyTimeoutWhileAnotherCon
195195
sprintf( 'mysql-on-sqlite:path=%s;dbname=wp', $this->db_path ),
196196
null,
197197
null,
198-
array( 'sqlite_timeout' => 0 )
198+
array( 'sqlite_busy_timeout' => 0 )
199199
);
200200

201201
$this->expectException( PDOException::class );

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,30 +379,27 @@ public function test_journal_mode_and_synchronous_driver_options(): void {
379379
}
380380
}
381381

382-
public function test_timeout_driver_option_sets_sqlite_busy_timeout(): void {
382+
public function test_busy_timeout_driver_option(): void {
383383
$driver = new WP_MySQL_On_SQLite(
384384
'mysql-on-sqlite:path=:memory:;dbname=wp',
385385
null,
386386
null,
387387
array(
388-
'sqlite_timeout' => 5,
388+
'sqlite_busy_timeout' => 2500,
389389
)
390390
);
391391

392-
// The PDO SQLite driver does not support reading "PDO::ATTR_TIMEOUT"
393-
// back with "getAttribute()", but the SQLite busy timeout reflects it
394-
// in milliseconds.
395392
$this->assertSame(
396-
5000,
393+
2500,
397394
(int) $driver->get_sqlite_pdo()->query( 'PRAGMA busy_timeout' )->fetchColumn()
398395
);
399396
}
400397

401-
public function test_timeout_defaults_to_the_connection_default(): void {
398+
public function test_busy_timeout_defaults_to_the_connection_default(): void {
402399
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp' );
403400

404401
$this->assertSame(
405-
WP_SQLite_Connection::DEFAULT_SQLITE_TIMEOUT * 1000,
402+
WP_SQLite_Connection::DEFAULT_SQLITE_BUSY_TIMEOUT,
406403
(int) $driver->get_sqlite_pdo()->query( 'PRAGMA busy_timeout' )->fetchColumn()
407404
);
408405
}

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public function db_connect( $allow_bail = true ) {
453453
try {
454454
$options = array(
455455
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
456-
'sqlite_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
456+
'sqlite_busy_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
457457
);
458458
$dbh = new WP_MySQL_On_SQLite(
459459
sprintf(

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/install-functions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function sqlite_make_db_sqlite() {
3030
null,
3131
array(
3232
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
33-
'sqlite_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
33+
'sqlite_busy_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
3434
)
3535
);
3636
$translator->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO

0 commit comments

Comments
 (0)