Skip to content

Commit 7c07787

Browse files
Merge pull request rails#49352 from fractaledmind/ar-sqlite-retries
Allow SQLite3 `busy_handler` to be configured with simple max number of retries
2 parents d99e355 + 236a144 commit 7c07787

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Allow SQLite3 `busy_handler` to be configured with simple max number of `retries`
2+
3+
Retrying busy connections without delay is a preferred practice for performance-sensitive applications. Add support for a `database.yml` `retries` integer, which is used in a simple `busy_handler` function to retry busy connections without exponential backoff up to the max number of `retries`.
4+
5+
*Stephen Margheim*
6+
17
* The SQLite3 adapter now supports `supports_insert_returning?`
28

39
Implementing the full `supports_insert_returning?` contract means the SQLite3 adapter supports auto-populated columns (#48241) as well as custom primary keys.

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,16 @@ def reconnect
712712
end
713713

714714
def configure_connection
715-
@raw_connection.busy_timeout(self.class.type_cast_config_to_integer(@config[:timeout])) if @config[:timeout]
715+
if @config[:timeout] && @config[:retries]
716+
raise ArgumentError, "Cannot specify both timeout and retries arguments"
717+
elsif @config[:timeout]
718+
@raw_connection.busy_timeout(self.class.type_cast_config_to_integer(@config[:timeout]))
719+
elsif @config[:retries]
720+
retries = self.class.type_cast_config_to_integer(@config[:retries])
721+
raw_connection.busy_handler do |count|
722+
count <= retries
723+
end
724+
end
716725

717726
raw_execute("PRAGMA foreign_keys = ON", "SCHEMA")
718727
end

0 commit comments

Comments
 (0)