Posted by Paolo
Mon, 14 Jan 2008 15:09:00 GMT
We’re planning our next RailsWorkshop, and we need you!
Where would you like it to be? What’s your most comfortable location? We set up a poll so you can express your preference.

Thanks for your vote!
Socialize it:
Posted in Ruby + Rails | Tags railsworkshop | 1 comment | no trackbacks
Posted by Paolo
Thu, 03 Jan 2008 20:53:00 GMT
Yesterday we spent all day dealing with a strange bug… something I want to share with you.
Basically we have an app which runs from CDROM and stores its data in a sqlite3 database file inside the user home. We manage the updates from a remote website downloading and swapping the sqlite database file on the fly.
What we do is something like this:
# download the new DB file
new_data = URI.parse('http://foo.com/new.db').read
File.open("#{home}/new.db", 'w') {|f| f.write(new_data)}
# connect to the downloaded DB file
ActiveRecord::Base.estabilish_connection :adapter => 'sqlite3',
:database => "#{home}/new.db"
# do something with the updated DB
# eg: read data for your updates and use it
# reconnect the original DB
ActiveRecord::Base.remove_connection
ActiveRecord::Base.estabilish_connection :production
# remove the downloaded DB file
rm "#{home}/new.db"
We tought that calling remove_connection would suffice to disconnect the new.db database, but when we tried to delete the file (the last line) we sadly discovered that under Windows it was kept locked by our own process even if no database connections were active on it. The problem seems to be inside the sqlite_adapter that once connected to a sqlite database, never calls the close method, leaving it locked forever.
So how do we close those open connections?
Thanks to ObjectSpace we can inspect all the sqlite database descriptors (SQLite3::Database instances) in our application and close them by hand. VoilĂ , this way we can safely delete our temporary database files, without cluttering our user’s home dir.
def disconnect_all_sqlite3
sqlite3_descriptors.each {|c| c.close }
end
def sqlite3_descriptors
open_descriptors = []
ObjectSpace.each_object(SQLite3::Database) {|x| open_descriptors << x}
open_descriptors
end
Just put a disconnect_all_sqlite3() call before the ActiveRecord::Base.estabilish_connection :production and everything will start working as it should!
Socialize it:
Posted in Ruby + Rails | Tags Ruby On Rails, sqlite3 | 2 comments | no trackbacks