withTransaction

fun <R> withTransaction(type: TransactionType? = null, connection: SQLiteConnection? = null, work: () -> R): R

Executes the given block of work in a database transaction.

The transaction is committed when the block completes successfully. If an exception is thrown within the block, the transaction is rolled back automatically.

This method will use the most recently opened connection if connection is null.

Example

val userId = myDatabase.withTransaction {
MyTable.quickInsert {
it.name.bind("User 1")
it.age.bind(18.0)
}
// return last inserted id
lastInsertRowId()
}

Return

The result of the work block.

Author

MOHAMMAD AZIM ANSARI

Parameters

type

The type of transaction to begin. If null (the default)

connection

The specific SQLiteConnection to use for the transaction. If null, the default cached connection for this database instance is used.

work

The block of operations to be executed in transaction.

See also

Throws

if database is not open or if an error occurs during the transaction.