Search code examples
kotlinkotlin-exposed

How can we determine, that we are already in transaction block?


I use exposed without DAO with caffeine cache. The common usage is

transaction {
  val row = rowService.getById(1)!! 
} 

RowService tries to find row in cache first, if negative, runs query againt database. I suspect that transaction block even without any queries has some overhead and if I cache-hit most of the time, I think to move transaction block in RowService where it resolves data from database, however I don't want to create nested transaction in the process.

How can I check that I'm in transaction block already and re-use it or open new one and run my fetch statementand?


Solution

  • By default nested transaction blocks will reuse the outermost Transaction instance. But if you want to ensure that there is a started Transaction from a code you could check it with: TransactionManager.currentOrNull() != null