Do we need to use a synchronized block for Room database

64 Views Asked by At

I have some code that I would like to use something like a threadpool because I am making multiple transactions at once. I used to use a SQLite database with a synchronized block. I figure that Room handles multi-threaded behaviour for you. Is that correct?

1

There are 1 best solutions below

0
On

I figure that Room handles multi-threaded behaviour for you. Is that correct?

No it does not.

  • Room is a wrapper around SQLite, it does, by default, not allow you to use the main thread (using .allowMainThreadQueires when building the database allows the restriction to be circumvented). The end result is still an SQLite database according to the SQLite version the resultant device is shipped with/has installed.

  • Room does implement some additions, such as the room_master_table (which stores a hashed value that is compared at run time against the compiled hash to detect if changes have been made (the hash is a hash of the schema, that Room generates according to the @Entity annotated classes defined to the @Database annotation)).