Never close Android SQLite connection-ThrowExceptions

Exception or error:

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?

How to solve:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *