I am not an expert, but I've had similar problems so let me give some
guidance.
The intermittent nature is strongly indicative of a locking problem.
You did not mention whether the SQL is read-only (select) or it does
writes. In cases where a lock is required, be advised that Sqlite
locks the whole database not just the table or records that you are
working with. Queries will not cause locking.
The Gears team recommend you to have one worker dedicated to SQL
writes. Since this is a single-thread, locking activity is sure not to
be executed in parallel. They give some sample code.
Do you declare var db = google.gears.factory.create('beta.database');
in every function where it is needed ? or pass the db variable between
functions ? i have tried both and found both work but the latter is
more robust.
I have seen it written that it up to the application developer to re-
try a failed query. I don't have any long-running updates, but I've
never found this to be necessary.