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

后端 未结 4 677
清歌不尽
清歌不尽 2020-12-01 03:56

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

4条回答
  •  悲哀的现实
    2020-12-01 04:02

    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();
        }
    

提交回复
热议问题