I'm loading the contents of a database into a GridView using a CursorLoader and a ContentProvider.
The application is working but in the log is written several times "close() was never explicitly called on database.
I found some topics about this issue and the response to "close database in ContentProvider 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".
So, whats going wrong?
As the database is opened within the ContentProvider, if I close it the cursor returned will be empty, how I resolve this issue?
That message is printed when the db is garbage collected before being closed. There is a lot of discussion about the issue here:
Closing the database in a ContentProvider
tl;dr: don't worry about it. It is, in general, actually counter-productive to close the db. Close your transactions and your cursors and let android manage the DB.
BTW, take care opening the DB in onCreate. If you open your DB using a SQLiteDatabaseHelper, the onUpdate method might be called. There isn't too much that could be worse than having your program killed by an ANR, in the middle of updating the DB.
Related
I am working on a chat application and in chat we are getting lots of update from server. And we are also saving the updates into the local SQLite database file.
Can someone suggest me, after every single insertion/update, should we close the database or we should close the database when it is actually needed ?
Closing the connection throws away the page cache, and requires that the schema version is checked and the entire schema is re-parsed the next time it is opened.
In most apps, there are not enough database accesses so that the overhead of continually re-opening the database would actually become noticeable. But this is no reason to add useless code to your app.
Please note that the SQLiteDatabase object is reference counted. So if you are using a global open helper instance, you can keep the DB open with an extra getWritableDatabase() call, even when all your other code calls close().
You should close the connection after each query.
In .net (and in most other frameworks), SQLConnections are stored in the background anyway. It won´t hit your runtime.
Also: connection pooling is your friend.
I have an android application in which I am using Content Provider on top of the database. However, I see that database gets created only when I insert the first record in the database through the Insert of Content Provider. In this scenario, if there's an error in database creation that would remain pending till the insertion of a record.
I would like to know if there's a way I can create the database when the app is accessed for the first time, so, any possible errors in the database creation appear at the earlier stage.
Within our app we show a splash screen specifically for the purpose of creating the db and initialising some application singletons. I'd recommend something similar.
You can start a db (on)Create or db (on)Upgrade by calling SQLiteDatabase#getWritableDatabase or equivalent. Remember to perform this on a background thread and use standard eventing / callbacks to understand when this (synchronous) method has completed.
If something goes wrong then you can catch that exception during start-up although you can't really recover by the sounds of it so you'll be best off not catching any exceptions and making sure your critical init code is bullet proof.
When I look at examples from tutorials I see that for getReadableDatabase the db is not closed at the end but for getWritableDatabase it is always closed at the end. Why is that? Even the docs specify that I must call close on getWritableDatabase. I have read both docs, so please do not simple quote the docs to me. Thanks.
Despite what the documentation implies, there isn't really a relevant difference between getReadableDatabase() and getWritableDatabase(); both open the database, and that database stays open until it is close()d.
So those examples are not correct; they should handle both cases in the same way.
Please note that "when you no longer need the database" does not necessarily mean that you need to close it after each query; if, for example, an activity is likely to access the database multiple times, it is perfectly valid to open it when the activity is started, and to close it when the activity is stopped.
An open database connection reserves some memory for its cache, and the system stops activities if it needs more memory, so you should ensure that there is no open database when there is no active activity.
You need to keep the database open as long as there are cursors in use that contain data from it. I myself rarely close the database, even when using getWriteableDatabase().
As per android documentation sqlite database
Writable database "Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close()when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed."
As the writable is cached as soon it's opened it means it will take up memory.
And if database is large then huge memory , so not closing it or keeping it open may lead to member leaks.
Hope this helps you :)
This title may sound a little bit crazy, but this is what is making me confused. My app heavily uses local database operations. As suggested in the Android docs and some blogs, I extended the SQLiteOpenHelper class and defined all my DB operations there. As some of my DB operations execute in threads, opening and closing of the db causes some IllegalStateExceptions. So, I made my DB helper as Singleton and it resolved those issues, also getting rid of the open and close operations for every DB action. Now everything seems to be working fine even though I never close the DB.
My confusion is that is it necessary to close DB?
If so, what is the right place to do so, is it in onDestroy of the main activity or somewhere else?
If I don't close DB, what are the side effects?
You can catch IllegalStateException if you'll try to open again the same database.
If you create instance of DBHelper in onCreate method of main activity - it would be write to close db in onDestroy. So, you can be sure, that next time in onCreate your database is not opened already.
If you have reference to DBHelper in service, than it should be opened and closed in service, and not in activity.
You can also use Application class for opening db, but than it will opened every time when you app starts (for example when you receive BroadcastReceiver)
The reason why you get exceptions is that you are trying to write/read from the same database via different threads.
I believe the best place to close your database would be inside the onDestroy() of your mainActivity.
I faced same problem.I opened database ,perform some operation and i forget to close.Actually i saw some exceptions i logcat that is "Leak found" db is opened and never closed.
We all learn that resources, such as database connections, should be acquired late and released early.
Yet applying this principle to SQLite database connections on Android have caused me some headache.
I have an app that download updates from a backend server in a service working in the background, writing updates to the database at a regular basis. The problem I experience arise when the following sequence occurs:
Service opens a writable database connection
Some activity opens a readable database connection
Service closes its database connection concurrently with the activity reading data
Activity fails due to its database connection was closed
Both the service and the activity uses the same SQLiteOpenHelper class, though different instances, to open their connections. My initial assumption was that this should work just fine, but somehow it seems that the underlying connection is shared between the two database instances.
To work around the problem I ended up not closing the database objects, only closing any opened cursors. This seems to work, though I'm not sure that I'm not leaking memory here.
Is there something obvious I am missing here?
Is there something obvious I am missing here?
I'd say no. Looking at the source code to SQLiteOpenHelper, I can't see how two instances could be sharing a SQLiteDatabase object.
Some diagnostic suggestions:
Dump the toString() value of each SQLiteDatabase, which should give you a Java instance ID. If they are the same, that is where your problem lies, and you will need to work your way upstream to figure out how the heck this is happening (e.g., you really are using the same instance of the SQLiteOpenHelper).
With your database in a stable state (i.e., no need to create or upgrade), flip one of your two database spots to use SQLiteDatabase directly, rather than via SQLiteOpenHelper, and see if that changes matters.