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
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")