Getting to know the state of transaction?

36 views
Skip to first unread message

Anto Aravinth

unread,
Apr 27, 2017, 2:05:14 AM4/27/17
to jOOQ User Group
Hi, 

I'm new to jooq and trying to do the following. I wanted to insert a data into the table. I'm running the following code in a transaction:

dslContext.transaction(configuration -> {
    DSLContext ctx = DSL.using(configuration);
    ctx
   .insertInto(USERDATA,USERDATA.USERNAME,USERDATA.PASSWORD)
   .values(username,password)
   .returning()
   .fetchOne();
});

Now I want to understand that within a transaction how will I come to know if the code inside it was successful or not? (only with exceptions). How to do that? Also let me know how to get to know about exceptions, say if an exception occurs on duplicate value etc. 

Thanks for your response. 


Lukas Eder

unread,
Apr 27, 2017, 2:23:48 AM4/27/17
to jooq...@googlegroups.com
Hi Anto,

Thanks for your message. jOOQ 3.9 added support for a TransactionListener SPI:


It will allow you to get a hold of the cause for any rollback:

I hope this helps,
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anto Aravinth

unread,
Apr 27, 2017, 2:50:19 AM4/27/17
to jooq...@googlegroups.com
Hi Lukas, 

Thanks for your response. But I have a question, if I implement the TransactionListener API, how should I pass that to the current executing transaction? Any examples repo will be really great. 

--
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/jtT55WSNu30/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+unsubscribe@googlegroups.com.

Lukas Eder

unread,
Apr 27, 2017, 3:08:50 AM4/27/17
to jooq...@googlegroups.com
You put that in your global Configuration, i.e the one you used to create your dslContext:

This isn't a per-transaction SPI, it's a global one, like the other SPIs in the Configuration.

Unfortunately, we currently don't have a manual section describing this SPI (will do soon). But it works like this:

Configuration global = new DefaultConfiguration()
   .set(connectionProvider)
   .set(transactionProvider)
   .set(dialect)
   .set(settings)
   .set(transactionListener);

DSLContext dslContext = DSL.using(global);

dslContext.transaction(configuration -> {
    /* transactional code here. Exceptions can be listened to from the above transactionListener */
});

Anto Aravinth

unread,
Apr 27, 2017, 3:32:52 AM4/27/17
to jooq...@googlegroups.com
Thanks Lukas, will look into that. 

Anto Aravinth

unread,
Apr 27, 2017, 3:46:16 AM4/27/17
to jooq...@googlegroups.com
Lukas, 

I was giving a thought about it. The pattern doesn't seems to be fitting in my head now. I'm using the pattern above in a REST call scenario. In first case, if there is a global listener, how will I return the exceptions to each caller? Imagine if there are 3,4 calls concurrently, even though the listener catches up the exception, I guess there is no way to throw that to the caller uniquely (so that I can show up the error message etc in the UI).

May be I'm wrong here. 

Anto Aravinth

unread,
Apr 27, 2017, 3:47:19 AM4/27/17
to jooq...@googlegroups.com
With that said, I guess it would be better to catch the exception and send back the same to caller. Probably that would be neat here. 

Lukas Eder

unread,
Apr 27, 2017, 5:45:51 AM4/27/17
to jooq...@googlegroups.com
I'm sorry I may have misunderstood your use-case.

jOOQ's transaction API doesn't swallow your exceptions. It just listens to them, rolls back your transaction, and propagates the exception to the caller. If the exception is a RuntimeException, it is propagated directly. If it is a checked exception, it is wrapped as a DataAccessException

Hope this helps,
Lukas

Anto Aravinth

unread,
Apr 27, 2017, 5:56:50 AM4/27/17
to jooq...@googlegroups.com
Yes, I got it, was reading the documentation. 

Thanks the library is really cool. It gives me the pleasure of writing SQL in Java, even though I'm relatively a noob in SQL world. 

Lukas Eder

unread,
Apr 27, 2017, 6:50:41 AM4/27/17
to jooq...@googlegroups.com
Speaking of documentation, I think the current behaviour deserves being documented in the Javadoc. I've created an issue for this:

Thanks for your nice words :)

Anto Aravinth

unread,
Apr 27, 2017, 7:49:19 AM4/27/17
to jooq...@googlegroups.com
Lukas, if you need I can help you with the above PR that you had created too :) 

Lukas Eder

unread,
Apr 27, 2017, 7:57:51 AM4/27/17
to jooq...@googlegroups.com
Thanks for your offer, Anto. That's alright - coordinating such a simple PR (transfer of rights agreement, meta-discussion, spell-checking, moving the PR to the "real repo" (GitHub is our secondary repo), making sure there are no accidental omissions, etc) is much more work than just doing it :)

But thanks anyway
Lukas
Reply all
Reply to author
Forward
0 new messages