Skip to content

Most visited

Recently visited

navigation
added in version 1.0.0
belongs to Maven artifact android.arch.persistence.room:common:1.0.0

Transaction

public abstract @interface Transaction
implements Annotation

android.arch.persistence.room.Transaction


Marks a method in a Dao class as a transaction method.

When used on a non-abstract method of an abstract Dao class, the derived implementation of the method will execute the super method in a database transaction. All the parameters and return types are preserved. The transaction will be marked as successful unless an exception is thrown in the method body.

Example:

 @Dao
 public abstract class ProductDao {
    @Insert
     public abstract void insert(Product product);
    @Delete
     public abstract void delete(Product product);
    @Transaction
     public void insertAndDeleteInTransaction(Product newProduct, Product oldProduct) {
         // Anything inside this method runs in a single transaction.
         insert(newProduct);
         delete(oldProduct);
     }
 }
 

When used on a Query method that has a Select statement, the generated code for the Query will be run in a transaction. There are 2 main cases where you may want to do that:

  1. If the result of the query is fairly big, it is better to run it inside a transaction to receive a consistent result. Otherwise, if the query result does not fit into a single CursorWindow, the query result may be corrupted due to changes in the database in between cursor window swaps.
  2. If the result of the query is a Pojo with Relation fields, these fields are queried separately. To receive consistent results between these queries, you probably want to run them in a single transaction.
Example:
 class ProductWithReviews extends Product {
     @Relation(parentColumn = "id", entityColumn = "productId", entity = Review.class)
     public List<Review> reviews;
 }
 @Dao
 public interface ProductDao {
     @Transaction @Query("SELECT * from products")
     public List<ProductWithReviews> loadAll();
 }
 
If the query is an async query (e.g. returns a LiveData or RxJava Flowable, the transaction is properly handled when the query is run, not when the method is called.

Putting this annotation on an Insert, Update or Delete method has no impact because they are always run inside a transaction. Similarly, if it is annotated with Query but runs an update or delete statement, it is automatically wrapped in a transaction.

Summary

Inherited methods

From interface java.lang.annotation.Annotation
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)