gpt4 book ai didi

android - 如何使用 green DAO 知道所有打开的数据库连接..?

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:42 30 4
gpt4 key购买 nike

大家好,我是 android 的新手并使用绿色 DAO 来管理数据库,但我经常遇到数据库未关闭的异常,因为我在应用程序 onTeriminate() 中这样做,我在整个应用程序中只维护一个数据库连接,但在某些情况下当我的应用程序进入后台并恢复数据库连接对象变为空时,我通过在使用前检查对象的空性来处理问题,但现在我经常遇到异常

android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

请帮我解决这个问题

提前致谢

Stack Trace : 01-03 09:39:18.688: E/SQLiteDatabase(3063): close() was never explicitly called on database '/data/data/com.opera.mini.android/databases/google_analytics.db' 01-03 09:39:18.688: E/SQLiteDatabase(3063): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1943) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:787) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.PersistentHitStore.loadExistingSession(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.PersistentHitStore.(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.PersistentHitStore.(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.AnalyticsReceiver.onReceive(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.opera.mini.android.OpMiniInstallReferrerReceiver.onReceive(Source:15) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.os.Handler.dispatchMessage(Handler.java:99) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.os.Looper.loop(Looper.java:137) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread.main(ActivityThread.java:4424) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at java.lang.reflect.Method.invokeNative(Native Method) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at java.lang.reflect.Method.invoke(Method.java:511) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at dalvik.system.NativeStart.main(Native Method) 01-03 09:39:18.688: E/System(3063): Uncaught exception thrown by finalizer 01-03 09:39:18.688: E/System(3063): java.lang.IllegalStateException: Don't have database lock! 01-03 09:39:18.688: E/System(3063): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090) 01-03 09:39:18.688: E/System(3063): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182) 01-03 09:39:18.688: E/System(3063): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178) 01-03 09:39:18.688: E/System(3063): at android.util.LruCache.trimToSize(LruCache.java:197) 01-03 09:39:18.688: E/System(3063): at android.util.LruCache.evictAll(LruCache.java:285) 01-03 09:39:18.688: E/System(3063): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143) 01-03 09:39:18.688: E/System(3063): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126) 01-03 09:39:18.688: E/System(3063): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914) 01-03 09:39:18.688: E/System(3063): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182) 01-03 09:39:18.688: E/System(3063): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168) 01-03 09:39:18.688: E/System(3063): at java.lang.Thread.run(Thread.java:856)

关闭连接的代码:

@Override
public void onTerminate() {
// TODO Auto-generated method stub
super.onTerminate();
snail_mail_db.close();
snail_mail_database_helper_obj.close();

}

最佳答案

您收到此异常“应用程序未关闭在此处打开的游标或数据库对象”,因为当您打开连接时,您需要在操作完成后关闭该游标。使用 db.close();完成任何操作后。

关于android - 如何使用 green DAO 知道所有打开的数据库连接..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947068/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com