Faithcaio Baum
Faithcaio Baum

Reputation: 63

jOOQ, asynchronous execution of Query

I was wondering if it is possible to execute an async delete or update Query directly with jOOQ.

dslContext.delete(MY_TABLE).where(...).execute();

I was only able to find a method for async fetching in ResultQuery

resultQuery.fetchLater();

Is there an easy way to do this?

Upvotes: 6

Views: 3867

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221106

Note, that as of jOOQ 3.10, as jOOQ still relies on JDBC, "asynchronous" calls merely delegate the blocking JDBC call to some executor asynchronously, but that executor will still block on the JDBC connection.

Oracle is currently working on a new, truly asynchronous API that complements JDBC. Once that stabilises, jOOQ will adapt to that as well, and will be able to run statements truly asynchronously in the database.

Meanwhile...

Post Java 8 / post jOOQ 3.8 solution

jOOQ 3.8 introduced support for Java 8's CompletionStage. You can now run:

CompletionStage<Integer> completion = ctx.delete(MY_TABLE).where(...).executeAsync();

You can also optionally pass an Executor to the executeAsync() method.

Pre Java 8 / pre jOOQ 3.8 solution

One way for you to perform asynchronous querying with jOOQ is by using Java's own concurrency APIs. For instance, you can write the following code:

final Query query = dslContext.delete(MY_TABLE).where(...);

ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(new Callable<Integer>() {
    @Override
    public Integer call() {
        return query.execute();            
    }
});

Note that the fetchLater() method was deprecated with jOOQ 3.2 as of #2581.

Upvotes: 7

Related Questions