Adding an option to override the default isolation level on per transaction basis (#1886)

This commit is contained in:
mkit 2017-10-16 14:03:51 +01:00 committed by GitHub
parent 2a68e23e69
commit 83b9080502

View File

@ -40,10 +40,21 @@ class CordaPersistence(var dataSource: HikariDataSource, private val schemaServi
}
}
fun createTransaction(): DatabaseTransaction {
/**
* Creates an instance of [DatabaseTransaction], with the given isolation level.
* @param isolationLevel isolation level for the transaction. If not specified the default (i.e. provided at the creation time) is used.
*/
fun createTransaction(isolationLevel: Int): DatabaseTransaction {
// We need to set the database for the current [Thread] or [Fiber] here as some tests share threads across databases.
DatabaseTransactionManager.dataSource = this
return DatabaseTransactionManager.currentOrNew(transactionIsolationLevel)
return DatabaseTransactionManager.currentOrNew(isolationLevel)
}
/**
* Creates an instance of [DatabaseTransaction], with the transaction isolation level specified at the creation time.
*/
fun createTransaction(): DatabaseTransaction {
return createTransaction(transactionIsolationLevel)
}
fun createSession(): Connection {
@ -53,9 +64,22 @@ class CordaPersistence(var dataSource: HikariDataSource, private val schemaServi
return ctx?.connection ?: throw IllegalStateException("Was expecting to find database transaction: must wrap calling code within a transaction.")
}
fun <T> transaction(statement: DatabaseTransaction.() -> T): T {
/**
* Executes given statement in the scope of transaction, with the given isolation level.
* @param isolationLevel isolation level for the transaction.
* @param statement to be executed in the scope of this transaction.
*/
fun <T> transaction(isolationLevel: Int, statement: DatabaseTransaction.() -> T): T {
DatabaseTransactionManager.dataSource = this
return transaction(transactionIsolationLevel, 3, statement)
return transaction(isolationLevel, 3, statement)
}
/**
* Executes given statement in the scope of transaction with the transaction level specified at the creation time.
* @param statement to be executed in the scope of this transaction.
*/
fun <T> transaction(statement: DatabaseTransaction.() -> T): T {
return transaction(transactionIsolationLevel, statement)
}
private fun <T> transaction(transactionIsolation: Int, repetitionAttempts: Int, statement: DatabaseTransaction.() -> T): T {