I have your answers! Never close the db. It is that simple Make sure you only have one, repeat one Helper instance per application.
Share it with all threads touchlab.co/blog/single-sqlite-connection Here's some more info on the internals of sqlite and locking: touchlab.co/blog/android-sqlite-locking.
I have your answers! Never close the db. It is that simple.
Make sure you only have one, repeat, *one Helper instance per application. Share it with all threads. touchlab.co/blog/single-sqlite-connection/ Here's some more info on the internals of sqlite and locking: touchlab.co/blog/android-sqlite-locking.
Thanks for the help. We are using one Helper instance. I forgot to clarify that the CalculonDatabase is an extension of the SQLiteOpenHelper and it is implemented as a Singleton (our one shared instance is grabbed with CalculonDatabase.getInstance()).
All threads are using that to get an instance of the database, but the close() seems to still give us trouble. – user1028181 Nov 3 at 18:27 Actually, looking at the first link you provided again, we have it implemented just like your example except we are closing the database when we write to it. Looks like we just have to stop calling close and trust the SQLite library to do its magic.
Thanks a ton. – user1028181 Nov 3 at 18:30 Just don't ever close it. You'll be fine.
Its really just a file handle. Not a big deal. Your app will close it naturally when it shuts down.As for database issues, its basically impossible to corrupt a Sqlite database unless you have a device with hardware issues.
– Kevin Galligan Nov 3 at 18:31 I need to update the wordpress theme. "They" is me ;) – Kevin Galligan Nov 3 at 18:32.
You don't need to bother. See this discussion which includes confirmation that closing the database is done by the system as needed if/when the process is killed off. The relevant quote, from a Google employee, is: A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database -- it will get closed as part of the kernel cleaning up the process's resources when the process is killed.
Unless I'm mistaken, this doesn't apply to the asker's situation. The asker seems to be working directly with the SQLiteDB, and not necessarily from within a ContentProvider. If he's getting an instance of the DB in an activity or a service, when that component gets destroyed, it must manually release the DB instance, since the process in which the component was running is likely still running the rest of the app.
– Slothsberry Nov 3 at 17:36 It doesn't make a difference. The point is, it's safe for the DB to never be manually closed. The system will close it if needed.
SQLite guarantees database integrity at any point, even if the device suddenly powers down without warning, so there's nothing important happening when you close anyway. Really, the best thing is to open the database connection once when your app starts up, and then forget about it. – Graham Borland Nov 3 at 20:31 I agree, that is likely the better practice.
However, if you don't want to create only a single databaseHelper(and instance), you'll need to be sure you close() before getting a new instance. It could be argued that having a monolithic singleton databaseHelper is worse than demanding that callers close() instances. It isn't necessarily the most well-founded position, but it could be argued.
– Slothsberry Nov 3 at 20:43.
If you are working directly on a SQLite database without resorting to a content provider, a pattern I have seen used is to close the database in Application. OnTerminate, whereas the Application instance stores the singleton database "adapter", which would be an object containing a SQLiteDatabase and its SQLiteOpenHelper.
I discovered in my project that the close() fuction in the SQLiteDatabase implementation on Android throws a NullPointerException when running multiple threads that open the database, insert data, and then close the database. Everything runs smoothly unless I allow each thread to close() after inserting into the database. You can see that at the end a call to close() is made.
When running multiple threads I get this exception. My question then is why does this error happen only when using the close() function? Also, is it ok to use close() at a much later time or possibly never?
Any tips with this issue are very much appreciated.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.