# How to install busy\_handler for SQLite3

**URL:** <https://www.ruby-forum.com/t/how-to-install-busy-handler-for-sqlite3/99604>\
**Category:** Rails\
**Created:** [July 24, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/how-to-install-busy-handler-for-sqlite3/99604 "2006-07-24T13:43:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [July 24, 2006, 1:43pm UTC](https://www.ruby-forum.com/t/how-to-install-busy-handler-for-sqlite3/99604/1 "2006-07-24T13:43:09Z")

</div>

I get too often SQLite3::BusyException and after searching a bit I  
think I’d need to install a busy\_hanlder to return true. That seems  
to be per connection, how would you do this in a global way in Rails?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [July 24, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/how-to-install-busy-handler-for-sqlite3/99604/2 "2006-07-24T19:13:41Z")

</div>

I did this using an abstract base class and it seemed to work for me.

There’s a SQLite driver call busy\_timeout() which just retries any  
busy lookup up to a certain time limit, for example busy\_timeout(2000)  
will retry up to 2 seconds.

In app/models, create retry\_model.rb:  
class RetryModel \< ActiveRecord::Base  
self.abstract\_class = true  
self.connection.raw\_connection.busy\_timeout(2000) if  
self.connection.adapter\_name == ‘SQLite’  
end

Then in all your models, change them to derive from retry\_model  
instead of ActiveRecord::Base, eg, chage:

class MyModel \< ActiveRecord::Base

to:

class MyModel \< RetryModel

That should make your BusyExceptions go away

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [July 24, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/how-to-install-busy-handler-for-sqlite3/99604/3 "2006-07-24T19:46:52Z")

</div>

On Jul 24, 2006, at 19:09, Michael Meckler wrote:

> I did this using an abstract base class and it seemed to work for me.
> 
> There’s a SQLite driver call busy\_timeout() which just retries any  
> busy lookup up to a certain time limit, for example busy\_timeout(2000)  
> will retry up to 2 seconds.

That’s great! That means it retries many times up to 2 seconds and  
then dies? Is that better than writing a busy\_handler that simply  
return true?

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [July 24, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/how-to-install-busy-handler-for-sqlite3/99604/4 "2006-07-24T21:08:56Z")

</div>

Yes, according to the SQLite API, calling busy\_timeout(2000) means  
SQLite will wait up to 2000 ms for a lock to clear before throwing a  
BusyException. By default, it throws the exception immediately.

A busy\_handler that just returns true defeats the purpose of busy  
locking. If there’s something keeping a lock on your database for more  
than two seconds, you want it to return an exception, because  
something is really wrong.
