Android Room database transactions

With the new Room Database in Android, I have a requirement where there are two sequential operations that needs to be made:


If I run this, I see (on examining the db) that there are some rows missing - I assume they are being deleted after inserting. viz. the first operation is running in parallel to the second.

If I use a transaction block, such as this, then it's all fine - the first operation seems to complete before doing the second:



It's also fine if I give a sleep in-between instead:



There doesn't seem to be much documentation for Room, and was wondering if I should use the transaction block like the above when I have sequential operations to be done, or is there any better way of doing it.

EDIT: After @CommonsWare pointed out, @Query are asynchronous, while @Insert and @Delete are synchronous. In view of this, how would I get a query which deletes rows to be async:

@Query("DELETE from table WHERE id IN(:ids)")
int removeRows(List<Long> ids);

According to the build output I get Deletion methods must either return void or return int (the number of deleted rows), if I try to wrap the return type in a Flowable.


as pointed out on documentation for Transaction, you can do following:

 public abstract class ProductDao {
    public abstract void insert(Product product);

    public abstract void delete(Product product);

    public void insertAndDeleteInTransaction(Product newProduct, Product oldProduct) {
         // Anything inside this method runs in a single transaction.

As @CommonsWare pointed out, @Query are asynchronous , while @Insert , @Delete , @Update are synchronous.

If you want to execute multiple queries in single transaction , Room also provides a method for that as mentioned below.

roomDB.runInTransaction(new Runnable() {
        public void run() {

I hope this will solve your problem.

I believe when we are using DAO interfaces, still we can perform transaction using default interface methods. We need to add the annotation @JvmDefault and @Transaction and we can perform any operation inside that, which belong to single transaction.

interface TestDao {
    fun insert(dataObj: DataType)

    fun update(dataObj: DataType): Completable

    fun delete(dataObj: DataType): Completable

    fun deleteAllData()

    fun getAllData(): Single<List<DataType>>

    fun singleTransaction(dataList: List<DataType>) {
        dataList.forEach {

here's the solution to this problem:

@Query("SELECT * FROM friend WHERE id = :id")
Friend getFriendByID(int id);
void delete(Friend friend);

Friend friendToBeDeleted = friendDAO.getFriendByID(id);

You have to go through two steps!

Need Your Help

What is the difference between Trusted_Connection and Integrated Security in a connection string?

sql-server connection-string

I'm curious what the difference between the token "Trusted_Connection" and "Integrated Security" in SQL Server connection strings (I believe other databases/drivers don't support these). I understand

Using a Filesystem (Not a Database!) for Schemaless Data - Best Practices

database filesystems nosql relational-database schemaless

After reading over my other question, Using a Relational Database for Schema-Less Data, I began to wonder if a filesystem is more appropriate than a relational database for storing and querying