I'm building an application where we're using a message queue.
New objects add their IDs to a beanstalkd queue via an after_create callback. Some daemons monitor beanstalk, and grab objects to process, as soon as they hit the queue.
We were getting errors from the daemons, saying "record with ID 'N' not found", but when we looked for object N via script/console, or the database, there it was.
It turns out that the after_create callback (along with all the other object creation callbacks) occurs *inside* a transaction. So, this is what was happening;
There are a couple of ways around this;
Call "self.class.connection.commit_db_transaction" in the model, after the after_create method. This works, but it smells really bad.
A nicer way is to add an "after_commit" callback, like this
Pat Allan has some modifications to this, to make it play nicer with Rails 2.0 and 2.1
Or, there is a version here which handles nested transactions.
Many thanks to Pat for showing me the after_commit stuff.
2008/07/01
ActiveRecord commit timing
Posted by Anonymous at 11:52 pm
Labels: activerecord, rails
Subscribe to:
Comment Feed (RSS)
|