-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQLiteDatabaseLockedException: database is locked (code 5) #191
Comments
It probably means you're creating multiple DbUpdateHelper instances. You almost always only want to make one, ever. |
Hi, Thanks for the help. Will using a singleton pattern solve this bug? |
If that's the cause, yes. I think you can (and should) just make a single DaoSession as well, so you can just make the session into a singleton. I forget exactly what goes into DaoSession though, so I could be wrong on that - check the docs :) |
I think I cannot make DbUpdateHelper singleton because the static instance will hold a reference to the context which will not get garbage collect until classloader has been unloaded which is I think almost never. Is there any other solution I can implement to prevent multiple instances of DbUpdateHelper. Although I still don't understand how is it even possible that there are multiple instances of DbUpdateHelper since I am using the code in the application class and not anywhere else. |
I got a similar crash, I have a singleton DatabaseHelper, and no transaction operation, but still crash |
you can use context.getapplicationcontext, then the context can be collect by the garbage |
Did you solve this issue? |
@michal-luszczuk |
@donghuipei |
@michal-luszczuk
|
@donghuipei |
I wonder what's the solution in case if multiple processes need accessing the same SQLite database... |
@artesa Based on a quick Google search using a ContentProvider is the common solution to access a (SQLite) database from another process/app. -ut |
@greenrobot-team Clever but nonconstructive, and I can explain why. SQLite documentation states that SQLite should support multiprocess access out-of-box. Using a ContentProvider is basically accessing SQLite database from a single process, while accessing the provider itself happens in a multiprocess manner. Therefore, it seems like the problem lies in Google's implementation of SQLiteOpenHelper, which makes multiprocess access done through it unreliable while you keep the connection open (and, of course, there is a plenty of other problems in case you don't). I reached some progress in struggling with the issue, but still was not able to get rid of it completely. That's why I'm interested whether anyone was more successful. BTW, I don't believe ContentProviders are actually a panacea. The library based on them which we used was generating even more errors on heavy loads than accessing SQLite directly. Was it the problem of the library? Maybe. But it's rather popular and we failed to find the reason in code, which itself makes providers either unreliable or hard to set up properly in some cases. As a generalization of your comment I could add the following: based on a quick Google search (hint) dividing your app into multiple processes is strictly discouraged in principle. Yep, that's true. But, would this knowledge help me to deal with that huge ton of legacy? Don't think so. :) |
I am also seeing this happen occasionally in an app that I work on, but the app is not designed to use multiple processes. |
I'm occurred this problem, so we do not use greendao for multi process? Thread Name: 'ModelWriteWorkThread' | | Back traces starts. ` |
@michal-luszczuk |
@xurui1995 You should check in which process code is called (using This comment ) and this should resolve your problems. |
if we need to read or write db in multi process, how we do to solve this problem? @michal-luszczuk @donghuipei |
I have the similar crash,just becouse,I write self Application but forget to set appliaction name in manifest.xml |
Hi, I hope this will help. |
Found how to solve it. The problem for me was that I created multiple instances of SQLiteOpenHelper for the same database file from multiple threads. getWritableDatabase() contains synchronized block inside to prevent parallel access to internal open() method from multiple threads but because as I said there were multiple instances of SQLiteOpenHelper this lock didn't work. I created singleton from SQLiteOpenHelper. In order to release memory, I used WeakReference for "instance" field. |
Some of my users are facing SQLiteDatabaseLockedException.
I am using this code in my application class onCreate method to setup database:
This is the only place where I am accessing the db using helper.getWritableDatabase().
Everywhere else I am using the daoSession object to access db.
Stacktrace:
The text was updated successfully, but these errors were encountered: