热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

关闭数据库是否必不可少?-Isitessentialtoclosethedatabase?

InGooglesNotepadExample,theydontseemtoclosethedatabase,atleastnotinonDestroy().在谷歌

In Google's Notepad Example, they don't seem to close the database, at least not in onDestroy().

在谷歌的记事本示例中,它们似乎不关闭数据库,至少不在onDestroy()中。

What is the purpose of closing it, and do I really need to? Does an open database take up significant memory? I have found that closing it in onDestroy leaves vulnerabilities if there are any threads running that might try to access it after the Activity is finished.

关闭它的目的是什么,我真的需要吗?开放数据库是否会占用大量内存?我发现在onDestroy中关闭它会留下漏洞,如果有任何运行的线程可能会在Activity完成后尝试访问它。

2 个解决方案

#1


10  

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. Run the Notepad example as-is and edit several notes in succession, you'll see that it starts throwing Warnings and Errors in LogCat. (In larger applications, you'll also start to see Memory Leak Detection warnings.)

记事本示例使用startManagingCursor,但您仍需要显式关闭数据库连接。按原样运行记事本示例并连续编辑几个注释,您将看到它开始在LogCat中抛出警告和错误。 (在较大的应用程序中,您也将开始看到内存泄漏检测警告。)

W/SQLiteCompiledSql(  302): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: INSERT INTO notes(body, title) VALUES(?, ?);
W/SQLiteCompiledSql(  302): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
W/SQLiteCompiledSql(  302):     at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:62)
...
W/SQLiteCompiledSql(  302):     at dalvik.system.NativeStart.main(Native Method)
E/Database(  302): close() was never explicitly called on database '/data/data/com.android.demo.notepad3/databases/data' 
E/Database(  302): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
E/Database(  302):  at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1810)
...
E/Database(  302):  at dalvik.system.NativeStart.main(Native Method)

You mentioned that closing it in onDestroy() "leaves vulnerabilities" in any non-UI threads that are still running. If you're using AsyncTask, you can check the status of of these threads using getStatus on your tasks.

你提到在onDestroy()中关闭它会在任何仍在运行的非UI线程中“留下漏洞”。如果您正在使用AsyncTask,则可以使用任务上的getStatus检查这些线程的状态。

if ( myAsyncTask.getStatus() == AsyncTask.Status.FINISHED ){
    mDbHelper.close();
}

Then close the connection in the onPostExecute method of your AsyncTask.

然后在AsyncTask的onPostExecute方法中关闭连接。

Hope that helps...

希望有帮助......

#2


0  

You should close it.

你应该关闭它。

The notepad example should be using Activity's startManagingCursor.

记事本示例应该使用Activity的startManagingCursor。


推荐阅读
author-avatar
c肀xc86_441
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有