mirror of
https://github.com/corda/corda.git
synced 2025-01-29 15:43:55 +00:00
Add additional transactionality check. (#4530)
* Add additional transactionality check. * Fix logic
This commit is contained in:
parent
82010fbb3e
commit
60388a7a45
@ -60,15 +60,19 @@ enum class TransactionIsolationLevel {
|
||||
val jdbcValue: Int = java.sql.Connection::class.java.getField(jdbcString).get(null) as Int
|
||||
}
|
||||
|
||||
internal val _prohibitDatabaseAccess = ThreadLocal.withInitial { false }
|
||||
|
||||
private val _contextDatabase = InheritableThreadLocal<CordaPersistence>()
|
||||
var contextDatabase: CordaPersistence
|
||||
get() = _contextDatabase.get() ?: error("Was expecting to find CordaPersistence set on current thread: ${Strand.currentStrand()}")
|
||||
get() {
|
||||
require(_prohibitDatabaseAccess.get() != true) { "Database access is disabled in this context." }
|
||||
return _contextDatabase.get() ?: error("Was expecting to find CordaPersistence set on current thread: ${Strand.currentStrand()}")
|
||||
}
|
||||
set(database) = _contextDatabase.set(database)
|
||||
|
||||
private val _prohibitDatabaseAccess = ThreadLocal.withInitial { false }
|
||||
|
||||
/**
|
||||
* The logic in the [block] will be prevented from opening a database transaction.
|
||||
* Also will not be able to access database resources ( Like the context transaction or the [contextDatabase] ).
|
||||
*/
|
||||
fun <T> withoutDatabaseAccess(block: () -> T): T {
|
||||
val oldValue = _prohibitDatabaseAccess.get()
|
||||
@ -131,8 +135,8 @@ class CordaPersistence(
|
||||
private val liveTransactions = ConcurrentHashMap<UUID, DatabaseTransaction>()
|
||||
|
||||
fun newTransaction(isolation: TransactionIsolationLevel = defaultIsolationLevel): DatabaseTransaction {
|
||||
if(_prohibitDatabaseAccess.get()){
|
||||
throw IllegalAccessException("Database access is not allowed in the current context.")
|
||||
if (_prohibitDatabaseAccess.get()) {
|
||||
throw IllegalAccessException("Database access is not allowed in the current context.")
|
||||
}
|
||||
val outerTransaction = contextTransactionOrNull
|
||||
return DatabaseTransaction(isolation.jdbcValue, contextTransactionOrNull, this).also {
|
||||
|
@ -10,11 +10,13 @@ import java.util.UUID
|
||||
import javax.persistence.EntityManager
|
||||
|
||||
fun currentDBSession(): Session = contextTransaction.session
|
||||
|
||||
private val _contextTransaction = ThreadLocal<DatabaseTransaction>()
|
||||
var contextTransactionOrNull: DatabaseTransaction?
|
||||
get() = _contextTransaction.get()
|
||||
get() = if (_prohibitDatabaseAccess.get() == true) throw IllegalAccessException("Database access is disabled in this context.") else _contextTransaction.get()
|
||||
set(transaction) = _contextTransaction.set(transaction)
|
||||
val contextTransaction get() = contextTransactionOrNull ?: error("Was expecting to find transaction set on current strand: ${Strand.currentStrand()}")
|
||||
val contextTransaction
|
||||
get() = contextTransactionOrNull ?: error("Was expecting to find transaction set on current strand: ${Strand.currentStrand()}")
|
||||
|
||||
class DatabaseTransaction(
|
||||
isolation: Int,
|
||||
|
Loading…
x
Reference in New Issue
Block a user