Skip to content

Commit 180dde4

Browse files
committed
add busyTimeout and set default busy timeout to 1 second
1 parent 26835ac commit 180dde4

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/database.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ int Database::EIO_Open(eio_req *req) {
157157
sqlite3_close(db->handle);
158158
db->handle = NULL;
159159
}
160+
else {
161+
// Set default database handle values.
162+
sqlite3_busy_timeout(db->handle, 1000);
163+
}
160164

161165
return 0;
162166
}
@@ -324,6 +328,17 @@ Handle<Value> Database::Configure(const Arguments& args) {
324328
Baton* baton = new Baton(db, handle);
325329
db->Schedule(RegisterProfileCallback, baton);
326330
}
331+
else if (args[0]->Equals(String::NewSymbol("busyTimeout"))) {
332+
if (!args[1]->IsInt32()) {
333+
return ThrowException(Exception::TypeError(
334+
String::New("Value must be an integer"))
335+
);
336+
}
337+
Local<Function> handle;
338+
Baton* baton = new Baton(db, handle);
339+
baton->status = args[1]->Int32Value();
340+
db->Schedule(SetBusyTimeout, baton);
341+
}
327342
else {
328343
return ThrowException(Exception::Error(String::Concat(
329344
args[0]->ToString(),
@@ -336,6 +351,16 @@ Handle<Value> Database::Configure(const Arguments& args) {
336351
return args.This();
337352
}
338353

354+
void Database::SetBusyTimeout(Baton* baton) {
355+
assert(baton->db->open);
356+
assert(baton->db->handle);
357+
358+
// Abuse the status field for passing the timeout.
359+
sqlite3_busy_timeout(baton->db->handle, baton->status);
360+
361+
delete baton;
362+
}
363+
339364
void Database::RegisterTraceCallback(Baton* baton) {
340365
assert(baton->db->open);
341366
assert(baton->db->handle);

src/database.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class Database : public EventEmitter {
149149

150150
static Handle<Value> Configure(const Arguments& args);
151151

152+
static void SetBusyTimeout(Baton* baton);
153+
152154
static void RegisterTraceCallback(Baton* baton);
153155
static void TraceCallback(void* db, const char* sql);
154156
static void TraceCallback(Database* db, std::string* sql);

0 commit comments

Comments
 (0)