Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database file is locked

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.

like image 340
Rafay Zia Mir Avatar asked Jan 20 '12 13:01

Rafay Zia Mir


3 Answers

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.

like image 152
jgauffin Avatar answered Oct 08 '22 14:10

jgauffin


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.

like image 33
Shahar Gvirtz Avatar answered Oct 08 '22 16:10

Shahar Gvirtz


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.

like image 20
Eric Yin Avatar answered Oct 08 '22 14:10

Eric Yin