tyczj
tyczj

Reputation: 73753

Multi threading with sqlite in windows store app

I have a little problem with my windows store app. The problem being a background task can pull data from a server and insert new data into the local database while at the same time the user can be navigating to a page that is pulling from that database and of course the app will crash because the database is locked, while this is a rare case, it does happen.

So the question is what are way I can combat this so that one connection will either wait for the other to finish or something like that as to not conflict with each other?

I am using sqlite-net as a sql wrapper class for all my stuff. Any suggestions?

Upvotes: 1

Views: 581

Answers (2)

Sandy
Sandy

Reputation: 1968

EkoostikMartin is correct. Using sqlite-net, you would want to do:

try {
    db.Execute ("PRAGMA journal_mode = WAL;");
} catch (SQLiteException e) {
    if (e.Result != SQLite3.Result.Row)
        throw e;
}

(Not sure if the catch is required on all platforms, but on WP8 this was necessary for me because the journal_mode PRAGMA returns a row, and the Execute call throws in that scenario)

Upvotes: 2

EkoostikMartin
EkoostikMartin

Reputation: 6911

You should try enabling WAL (Write Ahead Logging) mode. It allows for concurrent reading and writing.

http://www.sqlite.org/wal.html

SQLiteConnectionStringBuilder connBuilder = new SQLiteConnectionStringBuilder();
connBuilder.JournalMode = SQLiteJournalModeEnum.Wal;

Upvotes: 1

Related Questions