withSavepoint

fun <R> withSavepoint(savepointName: String, connection: SQLiteConnection? = null, work: () -> R): R

Executes the given block of work within a named savepoint savepointName.

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

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

Example

val result = myDatabase.withTransaction {
// Operations inside the main transaction

try {
myDatabase.withSavepoint("mySavepoint") {
// Perform operations that might fail
// If an exception is thrown here, only these operations are rolled back.
"Success from savepoint"
}
} catch (e: Exception) {
// The savepoint was rolled back, but the outer transaction is still active.
"Failed from savepoint"
}
}

Return

The result of the work block.

Author

MOHAMMAD AZIM ANSARI

Parameters

savepointName

A unique name for the savepoint.

connection

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

work

The block of operations to be executed in savepoint.

See also

Throws

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