This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on JTA and session.flush()

 
Lakshmi Dasari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just put it in a try-catch block?

Mark
 
Lakshmi Dasari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lakshmi Dasari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic