Faithcaio Baum Faithcaio Baum - 5 months ago 12x
Java Question

jOOQ, asynchronous execution of Query

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


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


Is there an easy way to do this?


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>() {
    public Integer call() {
        return query.execute();            

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