Forums Register Login

Query on JTA and session.flush()

+Pie Number of slices to send: Send
I have a query on the usage of container managed transactions and explicit usage of session.flush(). From what I understand, if I demarcate a method boundaries with CMT Required option, then JTA takes care of associating the current session with the transaction, beginning, commiting the transaction and flushing and closing the session at the end of the transaction.

Now, if I have a method marked with CMT, which performs two DML operations, say session.save(object) and session.update(object), both
the statements are executed to the database at the time of transaction commit. This results in transaction rollback if any of the statements fail.
Now if I would like to handle the failure of one of the statements exclusively, I am unable to do so as JTA is flushing my session at the end of transaction. Meaning the exception is not caught in the try/catch block wrapping session.save().

Can I force the session.flush() after session.save() in CMT? This will force the statemets to be executed to the DB giving me the chance to handle the failure. I have seen that this works quite well for me. as I can explicitly rollback the transaction by setting sessionContext.setRollbackOnly() which rollbacks both the DML statements.

I want to know if there are any repercussions of handling session directly in CMT that I am not forseeing? Is it safe to call session.flush() in CMT?
+Pie Number of slices to send: Send
Why not just put it in a try-catch block?

Mark
+Pie Number of slices to send: Send
I have try/catch blocks in DAO around session.save and session.update, but the exception is not caught there as the focus shifts to where the transaction boundaries are marked. Because of this datalayer exceptions are exposed to the facade layer rather than the DAO layer.
+Pie Number of slices to send: Send
You can always throw the exception up out of the DAO layer. I suggest wrapping it up in a custom Exception, call it something like DAOException.

Mark
+Pie Number of slices to send: Send
The method which has the transaction boundaries marked is a session bean method. The exception is getting caught in the session bean methods instead of the DAOs.
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1463 times.
Similar Threads
BMT Rollback Issue
session.save(Object myPojo) is not working
Question Regarding HFE / Bean things you can do within business methods - Page 199
rollback() and setRollbackOnly()
Handle exception in Facade
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:49:26.