在不断更新时,我在哪里关闭我的数据库?

时间:2012-09-27 10:05:02

标签: android service database-connection

在我的应用程序中,我有Contacts_sync.java类同步联系人和offline_Messages.java,它包含用户在离线时收到的消息,并且每五秒检查一次新消息。

现在问题在于应用程序启动,联系人同步服务开始,每隔五秒启动一次检查脱机消息的服务。

所以在这种情况下我如何设法关闭数据库。如果我在offlineMessage服务中关闭数据库,那么如果有任何正在进行的联系同步过程,那么它会给我数据库关闭错误。

这是我的Logcat

09-27 13:22:45.125: E/Database(6970): close() was never explicitly called on database 'sipchat.db' 
09-27 13:22:45.125: E/Database(6970): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-27 13:22:45.125: E/Database(6970):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
09-27 13:22:45.125: E/Database(6970):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:822)
09-27 13:22:45.125: E/Database(6970):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:856)
09-27 13:22:45.125: E/Database(6970):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:849)
09-27 13:22:45.125: E/Database(6970):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:556)
09-27 13:22:45.125: E/Database(6970):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
09-27 13:22:45.125: E/Database(6970):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-27 13:22:45.125: E/Database(6970):   at org.sipchat.sipua.ui.DatabaseHelper.<init>(DatabaseHelper.java:39)
09-27 13:22:45.125: E/Database(6970):   at org.sipchat.sipua.ui.Contact_sync.onCreate(Contact_sync.java:110)
09-27 13:22:45.125: E/Database(6970):   at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
09-27 13:22:45.125: E/Database(6970):   at android.app.ActivityThread.access$2500(ActivityThread.java:117)
09-27 13:22:45.125: E/Database(6970):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
09-27 13:22:45.125: E/Database(6970):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 13:22:45.125: E/Database(6970):   at android.os.Looper.loop(Looper.java:130)
09-27 13:22:45.125: E/Database(6970):   at android.app.ActivityThread.main(ActivityThread.java:3687)
09-27 13:22:45.125: E/Database(6970):   at java.lang.reflect.Method.invokeNative(Native Method)
09-27 13:22:45.125: E/Database(6970):   at java.lang.reflect.Method.invoke(Method.java:507)
09-27 13:22:45.125: E/Database(6970):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-27 13:22:45.125: E/Database(6970):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-27 13:22:45.125: E/Database(6970):   at dalvik.system.NativeStart.main(Native Method)

任何想法和建议将不胜感激 感谢

1 个答案:

答案 0 :(得分:0)

不要关闭数据库。 There is no need(无论如何,数据都会安全地写入持久存储中)。在应用程序启动时打开一次,并在整个应用程序的生命周期内重用相同的连接。

请注意,保留单个连接(可能是static类的Application成员)非常重要,这样它就不会触发垃圾收集器尝试清理时看到的警告消息它了。