Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction for 2 dao in Room is real?

In Room have 2 tables/Dao - PoolDao and AccountDao with relation one-to-many (one pool many accounts). I need first create and save pool and after create and save (one or more) accounts.

How in room create transaction which operates with two Dao? In documentation i find only transaction for only one dao - https://developer.android.com/reference/android/arch/persistence/room/Transaction

like image 802
abbath0767 Avatar asked Jun 07 '18 08:06

abbath0767


People also ask

How is a dao implemented in room?

A DAO can be either an interface or an abstract class. If it’s an abstract class, it can optionally have a constructor that takes a RoomDatabase as its only parameter. Room creates each DAO implementation at compile time.

Is it possible to have @transaction methods in room?

Transaction methods in room are methods marked with @Transaction annotation. Since you can not have non-abstract methods in interfaces, you need to use an abstract class instead of interface for DAO. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.

What are transactions in a dao recordset?

Use transactions in a DAO Recordset. A transaction is a set of operations bundled together and handled as a single unit of work. The work in a transaction must be completed as a whole; if any part of the transaction fails, the entire transaction fails. Transactions offer the developer the ability to enforce data integrity.

How do I suspend a DAO from a transaction?

@Transaction methods can also be suspending and they can call other suspending DAO functions: You can also call suspension functions from different DAOs inside a transaction: You can provide executors (by calling setTransactionExecutor or setQueryExecutor when you’re building your database) to control the threads they run on.


1 Answers

You can user runInTransaction option of your appDatabase object:

appDatabase.runInTransaction
{
    appDatabase.dao1.doWhatNeedsToBeDone()
    appDatabase.dao2.doWhatNeedsToBeDone()
}

You have example here.

like image 184
daneejela Avatar answered Sep 20 '22 08:09

daneejela