Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Never close Android SQLite connection

Tags:

android

What happens if you simply never close your connection? I know you get exceptions if you don't close it and try to open another one, but what happens if you have a global singleton, and you simply don't close it? Any idea?

like image 216
Kevin Galligan Avatar asked Aug 27 '11 01:08

Kevin Galligan


1 Answers

A rift is formed in the space-time continuum, threatening extinction of all life in the universe, until the day is saved by a bunch of humans in brightly-colored funny-looking outfits (including some with capes, if you can believe it).

Oh, no, wait. That was just a comic book.


Actually, nothing much happens. I have been told, by Googlers and other Android experts, that due to the way SQLite works (particularly its handling of transactions), nothing bad should happen if you fail to close the database.

In fact, if you implement a content provider, you will have this same effect, as content providers are not called with any sort of onDestroy() method, meaning you will never have an opportunity to close your database. A content provider will just live until the process is terminated. Personally, this really bugs me, which is one of the reasons I try to minimize my use of content providers.

like image 144
CommonsWare Avatar answered Oct 15 '22 04:10

CommonsWare