Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to close db connection on android? Every time after your operation finished or after your app exit

I have an android application that access local sqlite3 db frequently, for performance consideration so i always keep the connection open. But one of my friends recommended me to open/close the connection every operation.

1) What's your guys opinion on these two methods ? cons/pros. 2) I did some testing and find the DB connection doesn't have too much perf overhead. Does the performance overhead of DB connection varies on the size of the DB ?

like image 397
jim.huang Avatar asked Jan 30 '11 09:01

jim.huang


People also ask

When should I close DB connection?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

Should I keep DB connection open?

Absolutely it is safe to do this. This is how client-server applications work. If you are using a three-tier application, the application server will keep a pool of connections open anyway.

Is DB Close necessary?

If you don't close the database connections, they'll cause memory leaks over time. The Notepad example does use startManagingCursor , but you still need to explicitly close the db connection.

Why do DB close?

The DB->close() method flushes any cached database information to disk, closes any open cursors, frees any allocated resources, and closes any underlying files.


4 Answers

I don't know of any performance penalties in frequent closing/opening of the database (regardless of its size). I think the answer to this question also depends on what type of application is accessing the database.

Do you "re-query" the database a lot?
Then it seems rectified to keep it open.

Do you fetch different data each time you fetch something?
Again, it seems reasonable to leave it open (as you won't gain in caching the data instead).

Are there any other applications accessing the same database?
If there is a risk for concurrency or blocking issues, it might be wise to close the database after finished reading/writing from/to it.

Generally I would say that you might gain more in caching data than in leaving the database open (contra closing it) when optimizing for performance.

like image 131
dbm Avatar answered Sep 26 '22 05:09

dbm


If you are using an in memory database then your data will be discarded when you close the connection.

A bit of an edge case perhaps, but it just caught me out.

like image 37
David Sykes Avatar answered Sep 23 '22 05:09

David Sykes


The documentation says the connection can be open as long as you need it. And can be closed in onDestroy() method. Documentation link

Persisting Database Connection:

Since getWritableDatabase() and getReadableDatabase() are expensive to call when the database is closed, you should leave your database connection open for as long as you possibly need to access it. Typically, it is optimal to close the database in the onDestroy() of the calling Activity.

    @Override
    protected void onDestroy() {
        mDbHelper.close();
        super.onDestroy();
    }
like image 23
Seagull Avatar answered Sep 24 '22 05:09

Seagull


As an addition, opening & closing a connection so frequently might possibly cause you to experience notorious SQLite exceptions, if you access db from multiple threads.

See, if you access db from multiple threads even over a single connection and since those operation are not atomic, then you may try to update db which was closed just before by another thread.

like image 25
stdout Avatar answered Sep 24 '22 05:09

stdout