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:
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.
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.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Transaction\n\nSummary: [Inherited Methods](#inhmethods) \n\nTransaction\n===========\n\n| The `android.arch` Architecture Components packages are no longer maintained. They have been superseded by the corresponding [androidx.\\*](/jetpack/androidx/migrate) packages. See [androidx.room.Transaction](/reference/androidx/room/Transaction) instead.\n\n\n`\npublic\n\n\nabstract\n@interface\nTransaction\n`\n\n\n`\n\n\nimplements\n\nAnnotation\n\n\n`\n\n|-------------------------------------------|\n| android.arch.persistence.room.Transaction |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nMarks a method in a [Dao](/reference/android/arch/persistence/room/Dao) class as a transaction method.\n\n\nWhen used on a non-abstract method of an abstract [Dao](/reference/android/arch/persistence/room/Dao) class,\nthe derived implementation of the method will execute the super method in a database transaction.\nAll the parameters and return types are preserved. The transaction will be marked as successful\nunless an exception is thrown in the method body.\n\n\nExample: \n\n```\n @Dao\n public abstract class ProductDao {\n @Insert\n public abstract void insert(Product product);\n @Delete\n public abstract void delete(Product product);\n @Transaction\n public void insertAndDeleteInTransaction(Product newProduct, Product oldProduct) {\n // Anything inside this method runs in a single transaction.\n insert(newProduct);\n delete(oldProduct);\n }\n }\n \n```\n\n\nWhen used on a [Query](/reference/android/arch/persistence/room/Query) method that has a `Select` statement, the generated code for\nthe Query will be run in a transaction. There are 2 main cases where you may want to do that:\n\n1. 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](https://developer.android.com/reference/android/database/CursorWindow.html), the query result may be corrupted due to changes in the database in between cursor window swaps.\n2. If the result of the query is a Pojo with [Relation](/reference/android/arch/persistence/room/Relation) fields, these fields are queried separately. To receive consistent results between these queries, you probably want to run them in a single transaction.\n\nExample: \n\n```\n class ProductWithReviews extends Product {\n @Relation(parentColumn = \"id\", entityColumn = \"productId\", entity = Review.class)\n public List\u003cReview\u003e reviews;\n }\n @Dao\n public interface ProductDao {\n @Transaction @Query(\"SELECT * from products\")\n public List\u003cProductWithReviews\u003e loadAll();\n }\n \n```\nIf the query is an async query (e.g. returns a [LiveData](/reference/android/arch/lifecycle/LiveData) or RxJava Flowable, the transaction is properly handled when the query is run, not when the method is called.\n\n\nPutting this annotation on an [Insert](/reference/android/arch/persistence/room/Insert), [Update](/reference/android/arch/persistence/room/Update) or [Delete](/reference/android/arch/persistence/room/Delete) method has no\nimpact because they are always run inside a transaction. Similarly, if it is annotated with\n[Query](/reference/android/arch/persistence/room/Query) but runs an update or delete statement, it is automatically wrapped in a\ntransaction.\n\nSummary\n-------\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class\u003c? extends Annotation\u003e` | ` annotationType() ` | | ` abstract boolean` | ` equals(Object arg0) ` | | ` abstract int` | ` hashCode() ` | | ` abstract String` | ` toString() ` | ||\n\n-\n\n Annotations\n -----------\n\n - [ColumnInfo](/reference/android/arch/persistence/room/ColumnInfo)\n - [ColumnInfo.Collate](/reference/android/arch/persistence/room/ColumnInfo.Collate)\n - [ColumnInfo.SQLiteTypeAffinity](/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)\n - [Dao](/reference/android/arch/persistence/room/Dao)\n - [Database](/reference/android/arch/persistence/room/Database)\n - [Delete](/reference/android/arch/persistence/room/Delete)\n - [Embedded](/reference/android/arch/persistence/room/Embedded)\n - [Entity](/reference/android/arch/persistence/room/Entity)\n - [ForeignKey](/reference/android/arch/persistence/room/ForeignKey)\n - [ForeignKey.Action](/reference/android/arch/persistence/room/ForeignKey.Action)\n - [Ignore](/reference/android/arch/persistence/room/Ignore)\n - [Index](/reference/android/arch/persistence/room)\n - [Insert](/reference/android/arch/persistence/room/Insert)\n - [OnConflictStrategy](/reference/android/arch/persistence/room/OnConflictStrategy)\n - [PrimaryKey](/reference/android/arch/persistence/room/PrimaryKey)\n - [Query](/reference/android/arch/persistence/room/Query)\n - [RawQuery](/reference/android/arch/persistence/room/RawQuery)\n - [Relation](/reference/android/arch/persistence/room/Relation)\n - [SkipQueryVerification](/reference/android/arch/persistence/room/SkipQueryVerification)\n - [Transaction](/reference/android/arch/persistence/room/Transaction)\n - [TypeConverter](/reference/android/arch/persistence/room/TypeConverter)\n - [TypeConverters](/reference/android/arch/persistence/room/TypeConverters)\n - [Update](/reference/android/arch/persistence/room/Update)\n-\n\n Classes\n -------\n\n - [DatabaseConfiguration](/reference/android/arch/persistence/room/DatabaseConfiguration)\n - [InvalidationTracker](/reference/android/arch/persistence/room/InvalidationTracker)\n - [InvalidationTracker.Observer](/reference/android/arch/persistence/room/InvalidationTracker.Observer)\n - [Room](/reference/android/arch/persistence/room/Room)\n - [RoomDatabase](/reference/android/arch/persistence/room/RoomDatabase)\n - [RoomDatabase.Builder](/reference/android/arch/persistence/room/RoomDatabase.Builder)\n - [RoomDatabase.Callback](/reference/android/arch/persistence/room/RoomDatabase.Callback)\n - [RoomDatabase.MigrationContainer](/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)\n - [RoomWarnings](/reference/android/arch/persistence/room/RoomWarnings)\n - [RxRoom](/reference/android/arch/persistence/room/RxRoom)\n-\n\n Enums\n -----\n\n - [RoomDatabase.JournalMode](/reference/android/arch/persistence/room/RoomDatabase.JournalMode)\n-\n\n Exceptions\n ----------\n\n - [EmptyResultSetException](/reference/android/arch/persistence/room/EmptyResultSetException)"]]