How to find out all open database connections using green DAO ..?

Hello everyone iam new for android and use green DAO to manage the database, but I often get an unlocked database as I do this in the onTeriminate () application, supporting only one database connection through the application, but in some cases when my application goes over to the background and resumes connecting to the database, the object becomes null, I dealt with this problem by checking the null value of the object before use, but now I often get an exception that

android.database.sqlite.DatabaseObjectNotClosedException: the 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 called explicitly in the database '/data/data/com.opera.mini.android/databases/google_analytics.db' 01-03 09: 39: 18.688: E / SQLiteDatabase (3063): android.database.sqlite.DatabaseObjectNotClosedException: the application did not close the cursor or database object that was opened here 01-03 09: 39: 18.688: E / SQLiteDatabase ( 3063): in android.database.sqlite.SQLiteDatabase. (SQLiteDatabase.java:1943) 01-03 09: 39: 18.688: E / SQLiteDatabase (3063): on android.database.sqlite.SQLiteDatabase.openDatabase (SQLiteDatabase.java:1007) 01-03 09: 39: 18.688: E / SQLiteDatabase (3063): in android.database.sqlite.SQLiteDatabase.openDatabase (SQLiteDatabase.java:986) 01-03 09: 39: 18.688: E / SQLiteDatabase (3063): in 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.ap ps.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): in java.lang.reflect.Method.invoke (Method.javaPoint11) 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β–Ί51) 01-03 09: 39: 18.688: E / SQLiteDatabase (3063): in dalvik.system. NativeStart.main (native method) 01-03 09: 39: 18.688: E / System (3063): throw an exception raised by the finalizer 01-03 09: 39: 18.688: E / System (3063): java.lang.IllegalStateException: does not have a 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 $ FinalizerDa emon.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 to close the connection:

@Override public void onTerminate() { // TODO Auto-generated method stub super.onTerminate(); snail_mail_db.close(); snail_mail_database_helper_obj.close(); } 
+4
source share
2 answers

You get this exception "The application did not close the cursor or the database object that was open here" because when you open the connection, you need to close this cursor when your operation is completed. use db.close (); after completion of any operation.

0
source

I extend the Application class (asApp) and open and close the database connection. Then all my actions can access the database through theApp.getInstance().daoSession() without closing the database when the action is paused or ends. Android will exit with the App, which will close the database connection when the last action is completed.

0
source

All Articles