jooq

Querying the DB within the ExecuteListener.end event in jOOQ


I need to check some conditions after an insert statement was executed in a jOOQ project. For this, I've written a custom ExecuteListener, overriding its end(ExecuteContext) method.

Within the end method, I need to query for some meta data and access the data in the DB. Both requires me tho have a working connection. I want to execute for instance:

context.meta()
       .filterTables(t -> tables.stream().anyMatch(table -> Objects.equals(table.asTable().getName(), t.getName())))
       .getPrimaryKeys();

This works perfectly fine as long as jOOQ handles the connections. As soon as I use this approach in an environment where a connection pool is in place (Agroal in Quarkus in my case), the connection is closed at the time ExecuteListener.end is called.

I tried to get a second connection like this:

final ConnectionProvider provider = ctx.configuration().connectionProvider();
try (Connection conn = provider.acquire()) {
                final DSLContext dsl = DSL.using(conn, ctx.configuration().dialect());
                final List<UniqueKey<?>> primaryKeys = MetadataInformation.getPrimaryKeys(dsl, insertTable);
...
...
}

But this returns also connections which are in state closed.

Is there as way to get around this problem?

For the 'jOOQ-only' test I use a H2 database (v 2.3.232), for the Quarkus test a Postgres DB (v 17.4.1). We have org.jooq.pro:jooq:3.19.19 and org.jooq.pro:jooq-meta:3.19.19 on the classpath.

Footnote: My code will be used in Quarkus and non-Quarkus environments. I.e. if possible, I do not want to reference internals (like DataSource) to get a new connection.


Solution

  • Don't run this in ExecuteListener::end, which happens at the very end of the execution lifecycle, with resources no longer being available? Instead, just run this at the ExecuteListener::executeEnd event, which happens at the end of the actual query execution but prior to fetching / closing / etc.