sqlite3 busy_handler

R

Ruby Newbie

------=_Part_12756_31518440.1130169013684
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Having trouble getting the sqlite3 busy_handler to work. The documentation
at http://sqlite-ruby.rubyforge.org/sqlite3/ lists *busy_handler*( data=3Dn=
il
) {|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 =3D SQLite3::Database.new(....)
db.busy_handler(DbHandler)

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

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

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?

------=_Part_12756_31518440.1130169013684--
 
J

Jamis Buck

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
 
R

Ruby Newbie

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.tx=
t
# 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 =3D 0
while (i<10000) do
i+=3D1
updateStmt.execute(i)
logHandle.print "pass ",i," completed at ",Time.now,"\n"
end
# fileHandle.close
end

require "sqlite3"
db =3D SQLite3::Database.new('test.db')
# db.execute("drop table test")
db.execute("create table test (collision INTEGER)")
handler =3D DbHandler.new
db.busy_handler(handler.busy_handler(data=3Dnil,retries=3Dnil))
updStmt =3D db.prepare("update test set collision =3D ?")
forked =3D File.new("forked.txt","w")
forkedNot =3D File.new("forked-not.txt","w")
forkedPid =3D 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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top