Skip to content

Commit

Permalink
Support busy timeout setting for SQLite driver
Browse files Browse the repository at this point in the history
Some operations may not take place in parallel. In that case, by
default, SQLite will return a "database is locked" error. This creates
problems for multi-threaded applications that happen to do two such
operations at the same time. Most of the time, having one wait a little
while resolves the situation. The SQLite library makes this easy to set
up, providing a way to install a time-based busy handler.

This commit adds support for setting the busy timeout when creating a
new SQLite connection, and also defaults it to 10s, which will give a
much better experience to the average user who takes this module and
starts doing stuff with it from multiple threads. The previous default
(since we didn't call the `sqlite3_busy_timeout` function) was zero,
meaning that using multiple connection objects, one per thread, left
the user quite likely to run into the busy error.
  • Loading branch information
jnthn committed Sep 18, 2018
1 parent 6385e3e commit 75d58ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/DBDish/SQLite.pm6
Expand Up @@ -21,6 +21,9 @@ method connect(Str() :database(:$dbname)! is copy, *%params) {

my $status = sqlite3_open($dbname, $p);
if $status == SQLITE_OK {
given %params<busy-timeout> // 10000 {
sqlite3_busy_timeout($p, .Int);
}
DBDish::SQLite::Connection.new(:conn($p), :parent(self), |%params);
}
else {
Expand Down
5 changes: 5 additions & 0 deletions lib/DBDish/SQLite/Native.pm6
Expand Up @@ -75,6 +75,11 @@ sub sqlite3_close(SQLite)
is export
{ ... }

sub sqlite3_busy_timeout(SQLite, int32)
returns int32
is native(LIB)
is export
{ ... }

sub sqlite3_prepare_v2 (
SQLite,
Expand Down

0 comments on commit 75d58ee

Please sign in to comment.