faizal faizal - 4 months ago 18
Android Question

does getWritableDatabase() affect transactions

As i understand it, calling

SqliteOpendHelper.getWritableDatabase()
always returns the same instance of
SQLiteDatabase
. So would a transaction associated with it also remain unaffected?

Assume a single threaded environment and i use a singleton SQLiteOpenHelper instance. Suppose i have a long transaction :


  • I call
    SQLiteDatabase.beginTransaction()
    once.

  • DAO1 constructor calls
    SqliteOpendHelper.getWritableDatabase()
    .

  • DAO1 method performs a CRUD operation on it.

  • DAO2 constructor calls
    SqliteOpendHelper.getWritableDatabase()
    .

  • DAO2 method performs a CRUD operation on it.

  • ...on and on

  • I call
    SQLiteDatabase.endTransaction()
    once.



Do all the DAO operations get executed within that single transaction? I could not figure out from the source if multiple calls to
getWritableDatabase()
effects transactions in some way.

CL. CL.
Answer

Yes; if you wrap getWritableDatabase() within a transaction, the same database connection (i.e., the same SQLiteDatabase object) will always use a single transaction, and all operations done through it are in the same transaction.

Please note that SQLiteDatabase supports nested transactions.