Hibernate version: 3.2.0.ga
I know this is a topic that has been well covered, but I have a question that I have not been able to find a definitive answer for that I was hoping someone here could help with.
I'm implementing the open-session-in-view pattern in a Tapestry web application that my team is developing. The
Open Session in View page on the Hibernate web site states that a good approach is to use two transactions in one Session. My question pertains to creating a new transaction in the same Session after the first one has been rolled back.
For example, let's say I have a typical CRUD application to create new users for my application, and I want to be able to correctly handle the scenario where an administrator attempts to create a new user with a name that is already in use (a primary or unique key violation). In my request action handler I would attempt to commit the transaction, and catch the exception thrown from the database, and convert this into a validation error which states that the username is already taken.
Is it safe at this point to start a second transaction on the same Session that the first one was rolled back on, or should I be completely closing out this Session and creating a brand new one for the view to use? I've read in several places that Sessions should be closed and discarded after any Hibernate exception is thrown, but then again I've read some
forum posts where one of the Spring guys is saying that it's ok to use the session after a rollback if you are just initializing lazy collections.
Can someone with a bit more experience with this give me some advice? Currently the way I have implemented open-session-in-view makes it difficult to retrieve a new Session, but I can fix that, only I didn't want to go to all of that trouble unless absolutely necessary.
Any help/advice is greatly appreciated...