I see in the archives that folks have had some problems with crashes in
Mono.Data.Sqlite, and I just ran into the same problem today. I had a
routine that loads up a lot of objects from the database, and it would
intermittently crash at different places in the loading process, not always
in the same place. It died inside Mono.Data.Sqlite with
Mono.Data.Sqlite.SQLite3.Prepare
as the offending statement.

Since the error was intermittent, I can't tell you with 100% certainty that
it's entirely gone, but it used to crash 8 times out of 10, and it has not
crashed at all since I took the following steps.

First, I just tried to avoid using calls to the DB any more than I had to.
The app had a loading loop in one place where it executed the same simple
query about 200 times. Although a little ugly, the repeated queries no
unpleasant performance impact; but, changing it so that I got only one big
query and dealt with it in memory reduced the crashes by a bit (from every
single time I ran the app to once-in-a-while-it-works!). And, of course,
it's much more elegant now :).

Second, I avoided reusing connections. In another place, the code had been
loading up objects one row at a time, then issuing a second query to pull in
some child objects, sort of like this (obviously simplified and pseudo;
there were LOTS of child collections):

void LoadWidgets() {
  SqliteConnection conn = new SqliteConnection(str);

  while (reader.Read() ) {
    Widget w = new Widget();
    // do some stuff to make my new widget object
    IList<WidgetKids> children = GetChildren(conn, w.UniqueId);
   ....
}

IList<WidgetKids> GetChildren(SqliteConnection c, int id) {
  // uses the passed in connection to create another reader,
  // make up WidgetKids, and return
}

I think the intent of the code was to avoid creating multiple connections at
the same time, but apparently given the state of Mono.Data.Sqlite, having
two readers open on the same connection can also be bad. Once I removed this
and made it so that only one connection and one reader were ever open at one
time, the crashes stopped.

Note that (unless the OS is 'helping me out'), there's no multithreading
going on here, which is a sure path to pain in Sqlite; but something
quasi-concurrent may be happening under the hood.

I'm relatively new to this; I'm not sure if the reuse of connections above
is generally deprecated. But this code was running on Windows and Ubuntu
under some fairly intense tests and never failed; but Monotouch/iPad was
never happy.

Hope this helps someone along the way...

bjs

-- 
Brian Schuth
ALPHCE, Inc.
Eastport, ME
+1 207 370 2430
[email protected]


--
View this message in context: 
http://monotouch.2284126.n4.nabble.com/My-experience-solving-I-hope-Mono-Data-Sqlite-crash-problem-tp3616537p3616537.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to