Where can I close my database when it is constantly updated?

In my application, I have a Contacts_sync.java class that synchronizes contacts and offline_Messages.java, which contain messages that were received by the user when he was offline, and he checks for new messages every five seconds.

Now the problem is starting the application, starting the contact synchronization service, and every five seconds, another service is also launched to check offline messages.

So, in this case, I manage to close the database. If I close the database in the offlineMessage service, then if there is any current contact synchronization process, this will lead to a database error.

Here is my 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) 

Any idea and suggestions will be appreciated.
Thanks

0
source share
1 answer

Do not close the database. No need (in any case, the data will be safely written to the permanent storage as soon as possible). Open it once, when you start the application and repeat the same connection throughout the life of the application.

Note that it is important to maintain a single connection, perhaps as a static member of your Application class, so that it does not cause the warning message that you saw when the garbage collector tries to clear it.

0
source

All Articles