# ActiveRecord Sqlite Synchronization

**URL:** <https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682>\
**Category:** Rails\
**Created:** [May 5, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682 "2008-05-05T14:47:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/1 "2008-05-05T14:47:30Z")

</div>

Hey!

I’m using ActiveRecord with an SQLite Database.

It works great, but has one drawback. SQlite does’nt support multiple  
connection at one time.

Is there a mathod to synchronize ActiveRecord calls over different  
threads?

thx CK

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/2 "2008-05-05T15:33:54Z")

</div>

google for activerecord multi threaded  
I think it has something to do with allow\_concurrency.  
-R

On Mon, May 5, 2008 at 6:47 AM, Christian K.

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/3 "2008-05-05T16:39:49Z")

</div>

Roger P. wrote:

> google for activerecord multi threaded  
> I think it has something to do with allow\_concurrency.  
> -R
> 
> On Mon, May 5, 2008 at 6:47 AM, Christian K.

Hmmm doesn’t work form me. I still get a “busy-Exception”.

Other options?

thx

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/4 "2008-05-05T17:18:31Z")

</div>

allow\_concurrency “allows conconcurrency” on the connection (as  
opposed to the database), so 2 threads can execute on the same  
connection. If you’re trying to have 2 applications read from the  
same database (like having a script/runner do database stuff in the  
background while your server runs), this is simply not possible.  
Because SQLite effectively uses a flat-file, there is no way to have  
more than 1 connection to the same database – there is no way for one  
connection to know what the other is doing so as to avoid concurrent  
modifications resulting in data corruption.

On May 5, 9:39 am, Christian K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/5 "2008-05-05T17:56:55Z")

</div>

tekwiz wrote:

> allow\_concurrency “allows conconcurrency” on the connection (as  
> opposed to the database), so 2 threads can execute on the same  
> connection. If you’re trying to have 2 applications read from the  
> same database (like having a script/runner do database stuff in the  
> background while your server runs), this is simply not possible.  
> Because SQLite effectively uses a flat-file, there is no way to have  
> more than 1 connection to the same database – there is no way for one  
> connection to know what the other is doing so as to avoid concurrent  
> modifications resulting in data corruption.

i would need a queuing mechanism, that executes queries fifo.

Wouldn’t it be possible to achieve such a behaviour with a mutex, that  
synchronizes access to activerecord’s save and destroy methods?

ck

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/6 "2008-05-05T18:02:49Z")

</div>

On 5 May 2008, at 16:56, Christian K. wrote:

> > connection to know what the other is doing so as to avoid concurrent  
> > modifications resulting in data corruption.
> 
> i would need a queuing mechanism, that executes queries fifo.
> 
> Wouldn’t it be possible to achieve such a behaviour with a mutex, that  
> synchronizes access to activerecord’s save and destroy methods?

Not if you’re talking about separate mongrels (or rails instance of  
any sort). But doesn’t sqlite allow enough concurrency  
([File Locking And Concurrency In SQLite Version 3](http://www.sqlite.org/lockingv3.html)  
) ?

Fred

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/7 "2008-05-05T18:38:08Z")

</div>

Yes, your fifo/mutex concept is correct. You just have to ensure that  
2 active record instances don’t attempt to play with the database at  
the same time.

On May 5, 11:06 am, Christian K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/8 "2008-05-05T18:06:47Z")

</div>

Frederick C. wrote:

> On 5 May 2008, at 16:56, Christian K. wrote:
> 
> > > connection to know what the other is doing so as to avoid concurrent  
> > > modifications resulting in data corruption.
> > 
> > i would need a queuing mechanism, that executes queries fifo.
> > 
> > Wouldn’t it be possible to achieve such a behaviour with a mutex, that  
> > synchronizes access to activerecord’s save and destroy methods?
> 
> Not if you’re talking about separate mongrels (or rails instance of  
> any sort). But doesn’t sqlite allow enough concurrency  
> ([File Locking And Concurrency In SQLite Version 3](http://www.sqlite.org/lockingv3.html)  
> ) ?
> 
> Fred

I have one Ruby Process, that runs two threads.

In one of them runs a webrick server, one is a background task. Both  
threads operate on the same database.

I’ve done a quick prototype with a mutex and i seems to work; no more  
busy exceptions.

I still have to dig a bit deeper.

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 5, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/activerecord-sqlite-synchronization/136682/9 "2008-05-05T18:42:17Z")

</div>

You are correct that SQLite3 does provide “enough” concurrency;  
however, the active record decides to pitch a hissy-fit, throwing  
exceptions, when it encounters a locked db rather than waiting and  
trying again for a certain period.

On May 5, 11:01 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
