Solution to the problem that sqlite encounters database is locked (SQLITE_BUSY)

When I came to the company in the morning, my colleagues in the testing department said that when testing multi-user concurrent operations, they encountered the problem that sqlite reported that the database is locked.
The reason for the preliminary analysis is that when multiple users add, delete, and modify the sqlite database, the transaction is controlled. When one user adds, deletes, and modifies the database, the transaction is not completed. At this time, another user adds, deletes, and modifies the database, because the previous user transaction is not completed. , when the number of concurrent users is more, thread insecurity is more likely to occur, so the synchronized keyword is used on the method of adding, deleting and modifying the database (Note: because the database persistence framework was not used in the javase project at that time) public synchronized int addSecurities(SecuritiesInfo si) {…..}, after adding the thread lock, during the test, there is no database is locked problem when running the program with 20 threads. However, when running the program with 50 threads, the problem of database is locked appeared. At that time, I guessed that the reason might be the use of pure jdbc, so I continued to modify the code, and changed the database connection to the c3p0 connection pool, and then opened the thread after using the database connection pool. The database is locked problem did not occur in the 50-run program simulation test. However, when the number of threads reaches 100, the database is locked problem occurs again.
The final solution is to modify the mode of the sqlite database: pragma journal_mode = WAL, sqlite defaults to delete mode.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326990744&siteId=291194637