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

Micronaut TX with JOOQ throws NoTransactionException in method without @Transactional when other methods in class use it

Open matt-snider opened this issue 2 years ago • 8 comments

Expected Behavior

When using Micronaut's Transaction Management with JOOQ, we should be able to define methods both with and without @Transactional as in this example controller:

@Controller
public class TestController {
    private final DSLContext db;

    public TestController(DSLContext db) {
        this.db = db;
    }

    @Transactional
    @Get("/foo")
    public void foo() {
        db.execute("<some query>");
    }

    // Should still work despite not having @Transactional
    @Get("/bar")
    public void bar() {
        db.execute("<some query>");
    }
}

Actual Behaviour

When we mix methods with and without @Transactional we get the following error when calling the method without it:

io.micronaut.transaction.exceptions.NoTransactionException: No current transaction present. Consider declaring @Transactional on the surrounding method
	at io.micronaut.transaction.jdbc.TransactionalConnectionInterceptor.intercept(TransactionalConnectionInterceptor.java:65)
       // omitted
Caused by: io.micronaut.transaction.jdbc.exceptions.CannotGetJdbcConnectionException: No current JDBC Connection found. Consider wrapping this call in transactional boundaries.
	at io.micronaut.transaction.jdbc.DataSourceUtils.doGetConnection(DataSourceUtils.java:135)
	at io.micronaut.transaction.jdbc.DataSourceUtils.getConnection(DataSourceUtils.java:93)

As commented here by @dstepanov:

[...] the Jooq is injection the data source where getConnection is delegating to TransactionalConnectionInterceptor which is failing on the same problem. SpringTX is actually checking if the transaction is missing and returns a simple connection.

Steps To Reproduce

  1. Checkout example application
  2. Run gradle test --info
  3. Compare with spring-tx branch, where test succeeds

Environment Information

No response

Example Application

https://github.com/matt-snider/micronaut-tx-issues

Version

3

matt-snider avatar Mar 23 '22 08:03 matt-snider

@dstepanov Any thoughts on this one? From your comment it sounded like you had some idea of how this could be fixed. I could take a look if you point me in the right direction

matt-snider avatar Dec 13 '22 21:12 matt-snider