I’m currently grokking the SQLiteDatabase and related classes with the purpose of getting a better understanding of how it handles concurrency (my current solution works, but I got the impression I might actually be doing redundant work, since its done with the presumption that concurrency is not handled for me).
Right, about the android documentation for SQLiteOpenHelper states the following about the getWriteableDatabase method
Once opened successfully, the database is cached, so you can call this
method every time you need to write to the database. (Make sure to
call close() when you no longer need the database.)
It’s a little unclear to me what is being cached(so I currently have a wrapper that does the caching for me). So, what is going on here? Are the two SQLiteDatabase instances wrappers around a cached/static instance?
Also, how is the actual locking handled? If I have two separate SQLiteDatabase instances, created by the same SQLiteOpenHelper instance, are these then transactional safe? As in if i begin a transaction in exclusive mode on one transaction, and then start another transaction in another thread, on the other SQLiteDatabase instance, I would expect the second one to not start until the first one is done. Is this how it works?
The
SQLiteDatabaseobject is cached.Assuming you have just one
SQLiteOpenHelper, you cannot “create twoSQLiteDatabaseinstances with this method”. The secondgetWriteableDatabase()call returns the sameSQLiteDatabaseas does the first call.If you are only accessing the database from a single component (e.g., just one activity or just one service), use one
SQLiteOpenHelper(and, by extension, only oneSQLiteDatabase), held by that component. If you are accessing the database from multiple components, you will need to go with a singletonSQLiteOpenHelperinstance, either directly or by means of wrapping your database in aContentProvider.