I am working on an Application where i have to use multi threading. I have a single instance of SQLiteOpenHelper class throughout my application where i have methods to open and close database and some methods to insert and fetch data.
Now my problem is, I open database in First Thread and start transactions at the same time Second Thread also get the opened database object and start transactions.
The First thread complete its process and close the database on the other side my Second thread is working and the Second Thread gets that the database is closed.
Its some logical issue please someone help me to solve this problem how i can handle the closing the database in a proper way.
Thanks in advance
-
I am no expert here, but did you consider making the CRUD functions synchronized?rahul– rahul2012年10月12日 13:58:28 +00:00Commented Oct 12, 2012 at 13:58
-
use a contentProvider, i think it will put all your requests in the same threadnjzk2– njzk22012年10月12日 14:09:17 +00:00Commented Oct 12, 2012 at 14:09
2 Answers 2
I have a single instance of SQLiteOpenHelper class throughout my application where i have methods to open and close database and some methods to insert and fetch data.
You should only close the database when you are completely done with it, such as when the activity or service that created these threads is destroyed.
It is also not out of the question for you to simply never close the database. If you implement a ContentProvider, you will never close your database. While this may cause warnings in LogCat about leaking open database handles, SQLite's transactional nature means that failing to close a database should not cause any particular problems (e.g., failed to flush a buffer).
1 Comment
Normally I would just keep the SQLLite Android DB opened and close it at the end of the application, however that does not remove the initial problem.
I have found when the DB is not closed properly, than it might have a problem being opened on Android, so the simple never close the DB did not seem to work for me.
I wrap all the operations in transactions and the exception handler blocks.
Note that all the transactions need to be completed by the time we close the DB, so the close method waits for transactions to complete ( I have transaction counter as well for this ).
Here is a sample try catch transaction block I wrap all the DB operations in:
SQLPersistentStore extends the SQLiteOpenHelper getPersistentStore() { returns null when the DB is closed }..
void public operation(..){
SQLPersistentStore persistentStore = null;
try {
persistentStore = getPersistentStore();
persistentStore.beginTransaction();
... do the DB operations
persistentStore.setTransactionSuccessful();
} catch ( IllegalStateException e) {
...
Log.w(...
} catch (Exception e) {
Log.e(TAG, ...
throw new RuntimeException(e);
} finally {
if(persistentStore != null){
persistentStore.endTransaction();
}
}
When the DB is closed and you try an operation on it, you get IllegalStateException, and that is caught.