flypen
flypen

Reputation: 2585

Multiple threading questions when using sqlite transaction in Android

I use sqlite transaction in Android:

SQLiteDatabase database = sqlite_helper.getWritableDatabase();

database.beginTransaction();
...
database.setTransactionSuccessful();
database.endTransaction();

My questions are :
1. Should I place endTransaction() in finally code block like this:

try {
    database.beginTransaction();
    ...
    database.setTransactionSuccessful();
}
finally {
    database.endTransaction();
}

If there are exepctions during database operations, will the database be rolled back automatically without using "finally"?

  1. When the transaction is not ended, can other threads read or write the same database? I hear sqlite in Android is threading safe, but I are not sure with it. I guess there will be some problems during transaction. Is there an error raised if another thread writes the same database with the same connection?
    I ever found this error in my app, but I don't know whether it's related to the threading safe problem:

android.database.sqlite.SQLiteMisuseException: library routine called out of sequence:

, while compiling

Does anyone help me to answer these questions? Thanks a lot!

Upvotes: 2

Views: 3371

Answers (3)

Phil
Phil

Reputation: 1

Yes, you should use the finally block. Here is a simple, THREAD SAFE method I use:

/**
 * Call for multiple DB insertion transactions.  It is thread safe and fast!
 */
private synchronized void writeTransaction(Runnable task) {
    try {
        db.beginTransaction();
        task.run();
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

the synchronized keyword locks the method with its containing object, thus making it thread safe...

Upvotes: 0

CL.
CL.

Reputation: 180060

You should always put endTransaction() into a finally block (also see the docs). Otherwise, the database would not be able to notice than an exception has happened.

The only other way to end a transaction would be to close the connection, in which case SQLite automatically rolls back any active transaction.

As long as one connection writes to the database (which means that a transaction is active), no other connections can read or write. Therefore, you should take care not to forget to end transactions.

You should never write from multiple threads; what would happen if one threads ends the transaction while the other one is still writing?

Your SQLiteMisuseException might be related, or not; that's impossible to say without seeing the code.

Upvotes: 1

fullahouse
fullahouse

Reputation: 141

1.you should always place endTransaction in finally block

2.transaction in SQLite is thread safe,see the doc http://www.sqlite.org/atomiccommit.html

Upvotes: 5

Related Questions