Hi everyone.
I have a question concerning simultaneous sessions and transaction in the same thread using hibernate. I already asked the question on stackoverflow (https://stackoverflow.com/questions/24446629/multiple-simultaneous-hibernate-sessions-per-thread) but I didn't get any answer so I hope you can help me.
I'm working on a REST webservice that uses hibernate as an OR Mapper. I recently upgraded hibernate from 3.2 to 4.3 and got an error because my JTA transaction was rolled back when I wanted to commit it (described here: https://stackoverflow.com/questions/24411519/jtatransaction-localstatus-rolled-back-after-creation). Basically my transaction got rolled back somewhere and then I got an error when I wanted to use that session again (which is correct hibernate behavior).
I think I figured out why I got that error. It is because when I receive the request a long session is started. This session also opens a transaction that will be open for a longer period of time. Besides this long running session/transaction also some small session/transaction should be opened on the same thread. As far as I found out, all sessions get the same UserTransaction to work with and since the small transactions commit and rollback this transaction I run in the error described in my other post.
Since I'm working with a huge code base it is that easy to change the code so that all that side session run in a different thread (in case that would help) or to refactor the whole service so that only one transaction can be open at any time.
Actual Question starts hereIs there any possibility to start multiple simultaneous sessions/transactions in one Thread? If so, what would I have to do, to tell hibernate to do that? If this is not possible what way of accomplishing a similar behavior would you suggest?
Code snippet I use to create the sessionsCode:
Session session = sessionFactory.openSession();
CustomSessionWrapper dpSession = new CustomSessionWrapper(session, this);
if (!session.isClosed() && !session.getTransaction().isActive()) {
session.beginTransaction();
}
The code worked before I upgraded hibernate so the problem should not be in the CustomSessionWrapper or any other custom class.
Thanks a lot in advance!