0

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?

abi
  • 99
  • 8

2 Answers2

2

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

Tapac
  • 1,889
  • 1
  • 14
  • 18
0

Since Exposed 0.16.1 it is possible to use nested transactions. To enable this feature you should set useNestedTransactions on desire Database instance to true.

Just don't set useNestedTransactions (or better, explicitly set it to false) and you'll get the desired behavior.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487