Hi, When multiple users access my sqlite (v2) database through the rails app, I sometimes get a "busy" error message. I take it I somehow need to "lock" the database when its being accessed, and somehow make other things wait for access. How could I do that? Joe
On 5/23/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > When multiple users access my sqlite (v2) database through the rails > app, I sometimes get a "busy" error message. > > I take it I somehow need to "lock" the database when its being > accessed, and somehow make other things wait for access. > > How could I do that? >And shouldn''t the sqlite adapter be responsible for this? Or is that out of scope?
On 5/24/05, Michael Champanis <michael-MXk1+JRFB8SsTnJN9+BGXg@public.gmane.org> wrote:> > When multiple users access my sqlite (v2) database through the rails > > app, I sometimes get a "busy" error message. > > > > I take it I somehow need to "lock" the database when its being > > accessed, and somehow make other things wait for access. > > > > How could I do that? > > Hi Joe, > > Check out the ''[Rails] Need DB without root'' thread. > I''ve pasted the relevant information in case you don''t have it archived.Thanks! shouldn''t that be in the sqlite db adapter?> > Michael > > --paste-- > > >> sqlite locks the entire db during a transaction. this is not as bad > as it > >> sounds because it does some pretty smart things behind the scenes. > basically > >> you want something like > >> > >> max_tries = 42 > >> > >> max_tries.times do > >> begin > >> execute > >> break > >> rescue SQLite::LockedException > >> sleep rand > >> end > >> end > >> > >> let me know if that doesn''t make sense. > > > > > Would I put something like that around every model function that needed to > > update the DB? > > basically. i''d be more inclined just to redefine execute though; something > like: > > alias __execute__ execute > > def execute(*a, &b) > max_tries = Integer($max_tries || 42) > begin > return(__execute__(*a, &b)) > rescue SQLite::LockedException > (max_tries -= 1) > 0 ? (sleep(2 * rand) and retry) : raise > end > end >
> When multiple users access my sqlite (v2) database through the rails > app, I sometimes get a "busy" error message. > > I take it I somehow need to "lock" the database when its being > accessed, and somehow make other things wait for access. > > How could I do that?Hi Joe, Check out the ''[Rails] Need DB without root'' thread. I''ve pasted the relevant information in case you don''t have it archived. Michael --paste-->> sqlite locks the entire db during a transaction. this is not as badas it>> sounds because it does some pretty smart things behind the scenes.basically>> you want something like >> >> max_tries = 42 >> >> max_tries.times do >> begin >> execute >> break >> rescue SQLite::LockedException >> sleep rand >> end >> end >> >> let me know if that doesn''t make sense. >> Would I put something like that around every model function that needed to > update the DB?basically. i''d be more inclined just to redefine execute though; something like: alias __execute__ execute def execute(*a, &b) max_tries = Integer($max_tries || 42) begin return(__execute__(*a, &b)) rescue SQLite::LockedException (max_tries -= 1) > 0 ? (sleep(2 * rand) and retry) : raise end end