# Transaction

open class Transaction(transactionImpl: TransactionInterface) : UserDataHolder, TransactionInterface

Class representing a unit block of work that is performed on a database.

## Constructors

Transaction
Link copied to clipboard
constructor(transactionImpl: TransactionInterface)

## Types

Companion
Link copied to clipboard
object Companion

## Properties

connection
Link copied to clipboard
open override val connection: ExposedConnection<*>

The database connection used by the transaction.

currentStatement
Link copied to clipboard
var currentStatement: PreparedStatementApi?

The currently executing statement.

db
Link copied to clipboard
override val db: Database

The database on which the transaction tasks are performed.

debug
Link copied to clipboard
var debug: Boolean

Whether tracked values like statementCount and duration should be stored in statementStats for debugging.

duration
Link copied to clipboard
var duration: Long

The current total amount of time, in milliseconds, spent executing statements in this transaction.

id
Link copied to clipboard
val id: String

The unique ID for this transaction.

maxAttempts
Link copied to clipboard
var maxAttempts: Int

The maximum amount of attempts that will be made to perform this `transaction` block.

maxRepetitionDelay
Link copied to clipboard
var maxRepetitionDelay: Long
maxRetryDelay
Link copied to clipboard
var maxRetryDelay: Long

The maximum number of milliseconds to wait before retrying this `transaction` if an SQLException happens.

minRepetitionDelay
Link copied to clipboard
var minRepetitionDelay: Long
minRetryDelay
Link copied to clipboard
var minRetryDelay: Long

The minimum number of milliseconds to wait before retrying this `transaction` if an SQLException happens.

outerTransaction
Link copied to clipboard
open override val outerTransaction: Transaction?

The parent transaction of a nested transaction; otherwise, `null` if the transaction is a top-level instance.

queryTimeout
Link copied to clipboard
var queryTimeout: Int?

The number of seconds the JDBC driver should wait for a statement to execute in Transaction transaction before timing out. Note Not all JDBC drivers implement this limit. Please check the driver documentation.

readOnly
Link copied to clipboard
open override val readOnly: Boolean

Whether the transaction is in read-only mode.

repetitionAttempts
Link copied to clipboard
var repetitionAttempts: Int
statementCount
Link copied to clipboard
var statementCount: Int

The current number of statements executed in this transaction.

statements
Link copied to clipboard
val statements: StringBuilder

A StringBuilder containing string representations of previously executed statements prefixed by their execution time in milliseconds.

statementStats
Link copied to clipboard
val statementStats: HashMap<String, Pair<Int, Long>>

A mapping of previously executed statements in this transaction, with a string representation of the prepared statement as the key and the statement count to execution time as the value.

transactionIsolation
Link copied to clipboard
open override val transactionIsolation: Int

The transaction isolation level of the transaction, which may differ from the set database level.

warnLongQueriesDuration
Link copied to clipboard
var warnLongQueriesDuration: Long?

The threshold in milliseconds for query execution to exceed before logging a warning.

## Functions

addLogger
Link copied to clipboard
fun Transaction.addLogger(vararg logger: SqlLogger): CompositeSqlLogger

Adds one or more SqlLoggers to this transaction.

close
Link copied to clipboard
open override fun close()

Closes the transaction and releases any savepoints.

closeExecutedStatements
Link copied to clipboard
fun closeExecutedStatements()

Closes all previously executed statements and resets or releases any used database and/or driver resources.

commit
Link copied to clipboard
open override fun commit()

Saves all changes since the last commit or rollback operation.

exec
Link copied to clipboard
fun <T> exec(stmt: Statement<T>): T?

Executes the provided Statement object and returns the generated value.

fun <T, R> exec(stmt: Statement<T>, body: Statement<T>.(T) -> R): R?

Executes the provided Statement object, retrieves the generated value, then calls the specified function body with this generated value as its argument and returns its result.

fun exec(stmt: String, args: Iterable<Pair<IColumnType<*>, Any?>> = emptyList(), explicitStatementType: StatementType? = null): Unit?
fun <T : Any> exec(stmt: String, args: Iterable<Pair<IColumnType<*>, Any?>> = emptyList(), explicitStatementType: StatementType? = null, transform: (ResultSet) -> T?): T?

Executes the provided statement exactly, using the supplied args to set values to question mark placeholders (if applicable).

execInBatch
Link copied to clipboard
fun execInBatch(stmts: List<String>)

Provided statements will be executed in a batch. Select statements are not supported as it's impossible to return multiple results.

explain
Link copied to clipboard
fun Transaction.explain(analyze: Boolean = false, options: String? = null, body: Transaction.() -> Any?): ExplainQuery

Creates an ExplainQuery using the `EXPLAIN` keyword, which obtains information about a statement execution plan.

fullIdentity
Link copied to clipboard
fun fullIdentity(column: Column<*>): String

Returns the complete string identifier of a column, based on its Table.tableName and Column.name.

getOrCreate
Link copied to clipboard
fun <T : Any> getOrCreate(key: Key<T>, init: () -> T): T

Returns the value for the specified key. If the key is not found, the init function is called, then its result is mapped to the key and returned.

getUserData
Link copied to clipboard
fun <T : Any> getUserData(key: Key<T>): T?

Returns the value to which the specified key is mapped, as a value of type T.

identity
Link copied to clipboard
fun identity(column: Column<*>): String

Returns the string identifier of a column, based on its Column.name.

fun identity(table: Table): String

Returns the string identifier of a table, based on its Table.tableName and Table.alias, if applicable.

putUserData
Link copied to clipboard
fun <T : Any> putUserData(key: Key<T>, value: T)

Maps the specified key to the specified value.

registerInterceptor
Link copied to clipboard
fun registerInterceptor(interceptor: StatementInterceptor): Boolean

Adds the specified StatementInterceptor to act on this transaction.

removeUserData
Link copied to clipboard
fun <T : Any> removeUserData(key: Key<T>): Any?

Removes the specified key and its corresponding value.

rollback
Link copied to clipboard
open override fun rollback()

Reverts all changes since the last commit or rollback operation, or to the last set savepoint, if applicable.

unregisterInterceptor
Link copied to clipboard
fun unregisterInterceptor(interceptor: StatementInterceptor): Boolean

Removes the specified StatementInterceptor from acting on this transaction.

withDataBaseLock
Link copied to clipboard
fun <T> Transaction.withDataBaseLock(body: () -> T)

Creates table with name "busy" (if not present) and single column to be used as "synchronization" point. Table wont be dropped after execution.

withSuspendTransaction
Link copied to clipboard
suspend fun <T> Transaction.withSuspendTransaction(context: CoroutineContext? = null, statement: suspend Transaction.() -> T): T

Calls the specified suspending statement, suspends until it completes, and returns the result.