# TransactionScope

# TransactionScope

Artifact:[androidx.room:room-runtime](https://developer.android.com/jetpack/androidx/releases/room)  
[View Source](https://cs.android.com/search?q=file:androidx/room/Transactor.kt+class:androidx.room.TransactionScope)  
Added in[2.7.0](https://developer.android.com/jetpack/androidx/releases/room#2.7.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/room/TransactionScope "View this page in Java")
Common/AllAndroid/JVMNative/C/iOSJavaScript
  - Cmn  

  ```
  interface TransactionScope<T : Any?> : PooledConnection
  ```

*** ** * ** ***

A[PooledConnection](https://developer.android.com/reference/kotlin/androidx/room/PooledConnection)with an active transaction capable of performing nested transactions.  

|                                       See also                                        |
|---------------------------------------------------------------------------------------|---|
| [Transactor](https://developer.android.com/reference/kotlin/androidx/room/Transactor) |   |

## Summary

|                                     ### Public functions                                      |
|-----------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| `suspend `[Nothing](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-nothing/index.html) | [rollback](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#rollback(kotlin.Any))`(result: T)` Rollback the transaction, completing it and returning the[result](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#rollback(kotlin.Any)).                                                                                                                                                                                                                                                                                                         | Cmn |
| `suspend R`                                                                                   | `<R : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?> `[withNestedTransaction](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1))`(block: suspend `[TransactionScope](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope)`<R>.() `->` R)` Begins a nested transaction and runs the[block](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1))within the transaction. | Cmn |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ### Inherited functions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From[androidx.room.PooledConnection](https://developer.android.com/reference/kotlin/androidx/room/PooledConnection) |-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----| | `suspend R` | `<R : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?> `[usePrepared](https://developer.android.com/reference/kotlin/androidx/room/PooledConnection#usePrepared(kotlin.String,kotlin.Function1))`(sql: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`, block: (`[SQLiteStatement](https://developer.android.com/reference/kotlin/androidx/sqlite/SQLiteStatement)`) `->` R)` Prepares a new SQL statement and use it within the code[block](https://developer.android.com/reference/kotlin/androidx/room/PooledConnection#usePrepared(kotlin.String,kotlin.Function1)). | Cmn | |

## Public functions

### rollback

Cmn  

```
suspend fun rollback(result: T): Nothing
```

Rollback the transaction, completing it and returning the[result](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#rollback(kotlin.Any)).  

|                                                                                           See also                                                                                            |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [withTransaction](https://developer.android.com/reference/kotlin/androidx/room/Transactor#withTransaction(androidx.room.Transactor.SQLiteTransactionType,kotlin.coroutines.SuspendFunction1)) |   |
| [withNestedTransaction](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1))                              |   |

### withNestedTransaction

Cmn  

```
suspend fun <R : Any?> withNestedTransaction(block: suspend TransactionScope<R>.() -> R): R
```

Begins a nested transaction and runs the[block](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1))within the transaction. If[block](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1))fails to complete normally i.e., an exception is thrown, or[rollback](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope#rollback(kotlin.Any))is invoked then the transaction will be rollback, otherwise it is committed.

Note that a nested transaction is still governed by its parent transaction and it too must complete successfully for all its children transactions to be committed.

See also[Savepoint](https://www.sqlite.org/lang_savepoint.html)  

|                                                            Parameters                                                             |
|-----------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------|
| `block: suspend `[TransactionScope](https://developer.android.com/reference/kotlin/androidx/room/TransactionScope)`<R>.() `->` R` | The code that will execute within the transaction. |