简体   繁体   中英

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.

设置繁忙超时后, SQLite自动重试。

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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM