# TransactionScope

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

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

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/room3/PooledConnection` with an active transaction capable of performing nested transactions.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Transactor` |   |

## Summary

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

### Inherited functions

From [androidx.room3.PooledConnection](https://developer.android.com/reference/kotlin/androidx/room3/PooledConnection) `suspend R` `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/room3/PooledConnection#usePrepared(kotlin.String,kotlin.coroutines.SuspendFunction1)(sql: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, block: suspend (https://developer.android.com/reference/kotlin/androidx/sqlite/SQLiteStatement) -> R)`

Prepares a new SQL statement and use it within the code `https://developer.android.com/reference/kotlin/androidx/room3/PooledConnection#usePrepared(kotlin.String,kotlin.coroutines.SuspendFunction1)`.
Cmn

## Public functions

### rollback

Cmn

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

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

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Transactor#withTransaction(androidx.room3.Transactor.SQLiteTransactionType,kotlin.coroutines.SuspendFunction1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/room3/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 `https://developer.android.com/reference/kotlin/androidx/room3/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1)` within the transaction. If `https://developer.android.com/reference/kotlin/androidx/room3/TransactionScope#withNestedTransaction(kotlin.coroutines.SuspendFunction1)` fails to complete normally i.e. an exception is thrown, or `https://developer.android.com/reference/kotlin/androidx/room3/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 https://developer.android.com/reference/kotlin/androidx/room3/TransactionScope<R>.() -> R` | The code that will execute within the transaction. |