the program on which I am working gets data from a web service, adds data to database,delete and update. But sometimes when i run my application all the actions are successful but sometimes when i want to add,delete or update data I get this error.
Server was unable to process request. ---> The database file is locked database is locked
I cant understand the same program at some time works fine but at some time the very program gives this message.
SqlLite doesn't allow two threads to use the database at the same time.
The easiest way to solve it is to use the lock
statement around all database calls.
There is an excellent package called SysInternals. one of the component inside is Process Monitor (procmon). Use it on order to check which application locking your database files and if 2 instances of your application are "fighting" about getting lock on the DB files.
In Sqlite multiple processes can read from the DB files, but only one is allowed to make changes (INSERT/UPDATE/DELETE) at certain time. if more than one process is trying to change the DB in the same time you'll get this error. you can use Mutex to sync between the processes accessing the DB.
BTW, sqlite documentation recommends to avoid using the database from multiple threads. in order to make sure only one thread will access the DB at the same time, you can create a static class that will do all the interaction with the database and use a lock statements inside this class to avoid accessing the DB from multiple threads simultaneously.
Do you use file based database like Access?
Database file always locked (in almost all database system), please try stop database service to unlock the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With