Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jOOQ, asynchronous execution of Query

Tags:

java

sql

jooq

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?

like image 804
Faithcaio Baum Avatar asked Sep 05 '13 19:09

Faithcaio Baum


1 Answers

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.

like image 70
Lukas Eder Avatar answered Oct 22 '22 12:10

Lukas Eder