Running queries in CompletableFuture.runAsync

1,759 views
Skip to first unread message

Apoorv Naik

unread,
Feb 10, 2021, 5:20:17 PM2/10/21
to jOOQ User Group
I was experimenting with performing writes using the runAsync construct. What I have observed is that the statements in the runAsync have no knowledge of the writes that happened in the main thread that spawned these async tasks.

My question is around how to run async tasks under the same transaction context as the thread that is spawning the async tasks. 

Is this something that's doable? Is there a better approach for performing async writes ?

Lukas Eder

unread,
Feb 11, 2021, 6:22:04 AM2/11/21
to jOOQ User Group
You're probably running this code with a standard, thread-bound DataSource such as e.g. Spring is offering. This means that each of these async threads will spawn a new transaction, because that's how this DataSource works.

You will need to implement a DataSource, or a jOOQ ConnectionProvider that can handle this, or if you're using jOOQ's transaction API (async or not), the Configuration available to the transactional scope will always use the same Connection instance, which holds the transaction.

I'll be happy to review actual code if you're interested.

Thanks,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/36d068a7-f26d-4241-82a3-d322c71d38e8n%40googlegroups.com.

Apoorv Naik

unread,
Feb 11, 2021, 12:35:39 PM2/11/21
to jooq...@googlegroups.com
Yes, you're right, I'm using the standard spring data source backed by HikariCP and relying on JooqAutoConfiguration to do the rest. Do you have a detailed wiki or manual for creating a custom data source / connection provider that would support what I need?

I want to give it a shot before having you take a look at it.

Apoorv Naik


You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/ELuU4s9i1WI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/CAB4ELO6G3GR7SAyOYFamHk2pBzVta8ggh%3Dce8KOLGfRjc5H8pQ%40mail.gmail.com.

Lukas Eder

unread,
Feb 11, 2021, 1:40:36 PM2/11/21
to jOOQ User Group
No, we don't have such a wiki yet

Apoorv Naik

unread,
Feb 12, 2021, 1:48:34 AM2/12/21
to jooq...@googlegroups.com
Ok, let's try and fix the code together then.

@Override
@Transactional(propagation = Propagation.NESTED)
public Boolean save(Long version, ReconciliationResult reconciliationResult) {
....
}
So here's the main method that interacts with the DSL context object, from the Javadocs it looks like @Transactional doesn't support propagation to the transaction context to any spawned threads. 

I am guessing the best bet is to call the transaction API provided by Jooq, will the threads spawned from the dslContext.transaction(configuration -> { ..// threads spawned here }); have access to the same transaction.

Apoorv Naik


Lukas Eder

unread,
Feb 12, 2021, 2:27:37 AM2/12/21
to jOOQ User Group
I really recommend you don't use Spring's @Transactional annotation if you must use asynchronicity, as it will always bind transactions to threads. Some additional insight can be found here as well:

Yes, using the jOOQ transaction API is what I suggested. The "configuration" in your example will contain a DefaultConnectionProvider which will not return the Connection to the connection pool for as long as the transaction runs. Based on this "configuration" (and its underlying connection), you can now continue using jOOQ API, including the asynchronous one. Notice that depending on the thread safety guarantees of the underlying JDBC driver, you might still have to make sure your threads do not access the JDBC Connection concurrently. I.e. your asynchronicity should be implemented in a strictly sequential way.

Notice that the transaction itself will still block the calling thread. If you want to avoid that, you might as well just make the entire transaction asynchronous using DSLContext::transactionAsync, and then not worry about the transaction contents.

I hope this helps,
Lukas

Velmurugan A

unread,
Jun 16, 2023, 9:06:54 AM6/16/23
to jOOQ User Group
Hi Team,
I'm working in Spring boot. I need to pass my current Transaction to completableFuture thread. So that it could use on live data as well as it will roll back if any error in the main thread. please guide me that how can we do this ?

Lukas Eder

unread,
Jun 16, 2023, 9:10:18 AM6/16/23
to jooq...@googlegroups.com
Thanks for your message.

I'll be happy to answer specific questions (related to jOOQ) in case you run into issues. On such a high level, I don't think this is the right forum for your generic question about using Spring asynchronous transactions.

Thanks,
Lukas

Reply all
Reply to author
Forward
0 new messages