Taelia
Taelia

Reputation: 601

How do I retry reading a locked database with C#?

I have several C# applications that all use the same SQLite database (contains some user information). Only one of them has to write to it, but all of them now and then have to read.

While the one application is writing, if the other applications happen to want to read something, I get the Database Locked exception.

Is fine and all, and as expected, but how do I go around the retrying of the read? while (exception) try again is probably a naive and nasty solution..

I'm thinking there's simply some System.Core.Sqlite option about retries I'm missing. Google gave me some solutions that involved copying and deleting the database or killing processes; but I think those are quite.. unrelated.

Upvotes: 0

Views: 1617

Answers (2)

Helix 88
Helix 88

Reputation: 701

If possible you may want to investigate using Write-Ahead Logging (WAL).

Using WAL mode, a writer and readers do not block each other. However, there is still only one writer allowed, but this is not a concern as you are only writing to the DB using a single application.

Additionaly, you could catch this the System.Data.SQLite.SQLiteException and repeat the query until it works :-)

If brute force doesn't work you're not using enough :-)

Upvotes: 0

CL.
CL.

Reputation: 180210

SQLite automatically retries when you have set a busy timeout.

Upvotes: 1

Related Questions