How to know all open connections of data base usin

2019-04-09 09:59发布

问题:

Hi everyone iam new to android and using green DAO for managing database but iam frequently getting a database not closed exception as iam doing that in Application onTeriminate(),iam maintaining only single connection of database through out the Application but in some cases when my application goes to background and resumes data base connection object is becoming null, i handled the problem by checking null ness of object before using but now iam frequently getting the exception that

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

please help me how to handle this

thanks in Advance

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)

Code for Closing Connection :

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

}

回答1:

Your are getting this exception " Application did not close the cursor or database object that was opened here" because when you opene connection , you need to close that cursor when your operation is done. use db.close(); after completion of any operation.



回答2:

I extend the Application class (as theApp) and open and close the database connection there. Then all my activities can access the DB through the theApp.getInstance().daoSession() without closing the DB when the activity pauses or finishes. Android will terminate theApp, which will close the DB connection, when the last activity is finished.