How To Run Transactions?

We use the runTransaction to run queries inside of a transaction:

ctx.runTransaction {
  sql"""
    INSERT INTO customers(name)
    VALUES (1, 'abc')
  """.insert()
  sql"""
    INSERT INTO customers(name)
    VALUES (1, 'def')
  """.insert()
}

If one of the queries fails, the transaction will be rolled back, nothing will happen.


The runTransaction uses the default JDBC driver transaction isolation (depends on db).
If you want to explicitly set the transaction isolation you can use the runTransactionWithIsolation function:

ctx.runTransactionWithIsolation(TransactionIsolation.Serializable) {
  // queries here
}