Skip to content

Micronaut Data fails to evict an active connection when a @transactional method throws an exception extending Throwable, resulting in a new connection being opened for each subsequent request

License

Notifications You must be signed in to change notification settings

uberall/mn-kotlin-transactional-issue

Repository files navigation

Micronaut Transactional Issue with Kotlin Suspend Functions

Issue with Micronaut's transactional annotation using Kotlin suspend functions when throwing an exception that extends kotlin.Throwable

Versions

  • Micronaut 4.4.2
  • Kotlin 1.9.23
  • Kotlin coroutines 1.8.0
  • Java 21

Running locally

  • Run the test to reproduce the issue in another console ./gradlew -i test --rerun-tasks --tests "com.example.DemoTest.hikariIssue"
  • Changeing the variable callCount in DemoTest to a number below Hikari pool size will make the test pass

Possible root cause

Throwing an exception that extends kotlin.Throwable in a suspend function causes the transaction to be rolled back, but the connection is not released back to the pool.

We cannot reach the following part:

io.micronaut.transaction.async.AsyncUsingSyncTransactionOperations.withTransaction

result.whenComplete((o, throwable) -> {
    if (throwable == null) {
        synchronousTransactionManager.commit(status);
        newResult.complete(o);
    } else {
        try {
            synchronousTransactionManager.rollback(status);
        } catch (Exception e) {
            // Ignore rethrow
        }
        newResult.completeExceptionally(throwable);
    }
});

About

Micronaut Data fails to evict an active connection when a @transactional method throws an exception extending Throwable, resulting in a new connection being opened for each subsequent request

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages