micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

transaction context

Open vekonypeter opened this issue 1 year ago • 2 comments

Expected Behavior

inside a transaction (using @Transactional), the CoroutineCrudRepository.findAll method should return data that has changed inside the transaction, like all other built-in methods (e.g. findById) or custom queries do

Actual Behaviour

CoroutineCrudRepository.findAll does not recognise changes made inside that transaction, it returns the state as of the beginning of the transaction.

Steps To Reproduce

using:

  • Kotlin with coroutines
  • Micronaut Data with R2DBC
  • Postgres

sample code:

@Singleton
class TestSingleton(val itemRepo: ItemRepository) {

    val logger = logger { }

    suspend fun entry() {
        doTransaction()

        itemRepo.findAll().toList()
            .also { logger.info { "FIND ALL AFTER TRANSACTION: $it" } }
    }

    @Transactional
    suspend fun doTransaction() {
        val item = itemRepo.save(Item(name = "initial", number = 12))

        itemRepo.findAll().toList()
            .also { logger.info { "FIND ALL AFTER INSERT: $it" } }
        itemRepo.findById(item.id)
            .also { logger.info { "FIND BY AFTER INSERT: $it" } }

        itemRepo.update(item.copy(name = "updated", number = 66))

        itemRepo.findAll().toList()
            .also { logger.info { "FIND ALL AFTER UPDATE: $it" } }
        itemRepo.findById(item.id)
            .also { logger.info { "FIND BY AFTER UPDATE: $it" } }
    }
}

result:

10:52:24.282 [pool-1-thread-1 @coroutine#3] INFO  op.TestSingleton - FIND ALL AFTER INSERT: []
10:52:24.294 [pool-1-thread-1 @coroutine#3] INFO  op.TestSingleton - FIND BY AFTER INSERT: Item(id=1, name=initial, number=12)
10:52:24.306 [pool-1-thread-1 @coroutine#3] INFO  op.TestSingleton - FIND ALL AFTER UPDATE: []
10:52:24.308 [pool-1-thread-1 @coroutine#3] INFO  op.TestSingleton - FIND BY AFTER UPDATE: Item(id=1, name=updated, number=66)
10:52:24.314 [pool-1-thread-1 @coroutine#3] INFO  op.TestSingleton - FIND ALL AFTER TRANSACTION: [Item(id=1, name=updated, number=66)]

Environment Information

No response

Example Application

No response

Version

3.10.0

vekonypeter avatar Jan 23 '24 09:01 vekonypeter

Please try the latest version and also enable the SQL logging to see what's is going on.

dstepanov avatar Jan 23 '24 10:01 dstepanov