0

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

Now the problem is on application start-up, the service for contact synchronization starts and every five seconds another service for checking offline messages starts too.

So in this case how do i manage to close the database. If i close the database in offlineMessage service then if there is any ongoing process for Contact synchronization then it gives me database close 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

CocoNess
  • 4,213
  • 4
  • 26
  • 43
Juned
  • 6,290
  • 7
  • 45
  • 93

1 Answers1

0

Don't close the database. There is no need (data gets safely written to persistent storage at the earliest opportunity anyway). Open it once, on application startup, and reuse the same connection throughout your application's lifetime.

Note that it's important to keep a single connection, perhaps as a static member of your Application class, so that it doesn't trigger the warning message you saw when the garbage collector tries to clean it up.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • if i don't close the database then it gives me error which is shown in my Logcat. is it okay? and yeah i am using SQLite database to store the contacts and messages which is created by me. – Juned Sep 27 '12 at 10:12
  • Like I said, make sure you only ever open the database connection once on application startup, and reuse the same connection everywhere. Are you sure you're doing that? – Graham Borland Sep 27 '12 at 10:15
  • Nope in my current code,i have created so many instance of database and cursor and i opened and closed that database according to requirement, so multiple connections are there. – Juned Sep 27 '12 at 10:18
  • Well, that's why you're getting the error message. `Cursor` objects should be closed after you've finished using them, but the DB itself should be opened just once, and never closed. – Graham Borland Sep 27 '12 at 10:19
  • so we should declare the database object in a class that extends Application and open it once when required and reuse it every time? – G_S Sep 27 '12 at 10:21
  • Yes. I recommend opening it in your `Application.onCreate()` so you can be sure it's opened before anything else tries to use it. – Graham Borland Sep 27 '12 at 10:22
  • i never used this approach,do if is there any guidelines or tutorial then it will be very helpful to me – Juned Sep 27 '12 at 10:24
  • @SharathG can you provide me any sample for that? – Juned Sep 27 '12 at 11:06
  • @Graham Borland is oncreate a static method in application class? – G_S Sep 27 '12 at 11:43
  • No. http://developer.android.com/reference/android/app/Application.html#onCreate() – Graham Borland Sep 27 '12 at 11:58
  • But the application.oncreate() method called only once na when application installed? – Nency Sep 27 '12 at 12:15
  • No, it's called when the application starts. Please read the documentation. – Graham Borland Sep 27 '12 at 12:16