Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sqlite3 busy_handler

0 views
Skip to first unread message

Ruby Newbie

unread,
Oct 24, 2005, 11:53:47 AM10/24/05
to
Having trouble getting the sqlite3 busy_handler to work. The documentation
at http://sqlite-ruby.rubyforge.org/sqlite3/ lists *busy_handler*( data=nil
) {|data, retries| ...} as the public instance method. So far I've tried

Class DbHandler
print "in DbHandler\n"
def busy_handler(*data)
print "in DbHandler.busy_handler\n"
return true;
end
end

and registering using
db = SQLite3::Database.new(....)
db.busy_handler(DbHandler)

as well as
handler = DbHandler.new
db.busy_handler(handler)

as well as
db.busy_handler(handler.busy_handler(data=nil,retries=nil))

My first attempt with a method named busy_handler outside of a separate
class was no better.

The code compiles and runs, but two separate processes running in parallel
will lock the database - when I would expect them to both run indefinitely
since I am never returning false from the busy_handler method I'm trying to
invoke.

Any ideas?

Jamis Buck

unread,
Oct 24, 2005, 12:25:46 PM10/24/05
to
I'm afraid I have no idea whether the busy-handler callback actually
works or not. It was one of those things that I implemented because
it was in the SQLite3 API, but I didn't have a good way to unit test it.

- Jamis

Ruby Newbie

unread,
Oct 24, 2005, 3:39:45 PM10/24/05
to
Thanks - I was actually wondering if my class/method definition and
busy_handler invocation were correct. As for testing, here's a little
script I whipped up which generates a collision for me every time I
run it. But I still don't know if I'm defining or registering the
busy_handler correctly.

This runs in about 30 seconds for me on a Via C3 - it will probably
run in 5 - 10 seconds on a desktop PC/Mac.

++++++++++++++++++++++++++++++++Code below +++++++++++++++
#!/usr/local/bin/ruby

# author: Ruby Newb
# 24-oct-2005

# test sqlite3-ruby busy_handler
# create new sqlite3 db
# create table in db
# fork thread to update a field in the db, writing output to forked.txt
# execute loop to update a field in the db, writing output to forked-not.txt
# after 10000 iterations wait for the forked pid if still active

class DbHandler
print "in DBHandler\n"
def busy_handler (*data)
print "in DBHandler.busy_handler data is ",data,"\n"
return true;
end
end

def collide(updateStmt,logHandle)
i = 0
while (i<10000) do
i+=1
updateStmt.execute(i)
logHandle.print "pass ",i," completed at ",Time.now,"\n"
end
# fileHandle.close
end

require "sqlite3"
db = SQLite3::Database.new('test.db')
# db.execute("drop table test")
db.execute("create table test (collision INTEGER)")
handler = DbHandler.new
db.busy_handler(handler.busy_handler(data=nil,retries=nil))
updStmt = db.prepare("update test set collision = ?")
forked = File.new("forked.txt","w")
forkedNot = File.new("forked-not.txt","w")
forkedPid = fork { collide(updStmt,forked) }
begin
collide(updStmt,forkedNot)
rescue SQLite3::BusyException
print "unforked branch died first"
end
Process::waitpid(forkedPid,Process::WNOHANG)
db.execute("drop table test")

0 new messages