-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JTATransactionFactory and JTA transaction behavior/problem
PostPosted: Fri Jan 27, 2006 11:56 am 
Newbie

Joined: Fri Jan 27, 2006 11:34 am
Posts: 5
Hibernate version: 3.1.1
Application Server: WebSphere 6.0.2.5
Fragment of hibernate.cfg.xml
Code:
<property name="current_session_context_class">org.hibernate.context.JTASessionContext</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>


This code (Inside a servlet) works :
Code:
UserTransaction tx = (UserTransaction)newInitialContext().lookup("java:comp/UserTransaction");
tx.begin();
Session session = sessionFactory.getCurrentSession();
// Do some hibernate stuff here
tx.commit();


This (Inside a servlet) does not:
Code:
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
// Do some hibernate stuff here
tx.commit();

It fails on getCurrentSession() with:
Code:
org.hibernate.HibernateException: Current transaction is not in progress


In the doc, it is stated (section 3.8.1) that:
"org.hibernate.transaction.JTATransactionFactory delegates to container-managed transaction if an existing transaction is underway in this context (e.g. EJB session bean method), otherwise a new transaction is started and bean-managed transaction are used."


But this does not seems to work as expected. Obvisouly, for the failing case, there is no JTA transaction running, and calling Session.getCurrentSession() does not start a new one

Q:
1) Am I doing something wrong (config or code)?
2) Is the doc wrong?
3) Is it a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 12:44 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
no it is not a bug. Using EJB the transaction would be injected. Using simple Hibernate using JTA would will need to start a session. So you should call openSession instead of currentSession.
See my question to this topic a couple of days ago.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 5:38 pm 
Newbie

Joined: Fri Jan 27, 2006 11:34 am
Posts: 5
Thanks Sebastian for your answer. I've read your post (15 jan 2006) and the answers...and your solution seems to work but does not fully satisfy me...lol

In the doc, section 3.8.3, it is stated:...Using the "jta" session context, if there is no Hibernate Session associated with the current JTA transaction, one will be started and associated with that JTA transaction the first time you call sessionFactory.getCurrentSession()..
However, this section does not tell what happens when there is no JTA transaction in progress...

My understanding and experimentations with "jta" session context:
    - openSession(): starts a new JTA transaction and a new Session (What happens when a JTA Transaction is in progress?)
    - getCurrentSession():
      - if a session is running, return it
      - if ther is no session running, Hibernate tries to create one (section 3.8.3). If no JTA transaction is in progress , it will fail with exception "Current transaction is not in progress" If a JTA Transaction is running (Either created by the EJB container for EJB/CMT beans or manually for EJB/BMT or outside the EJB Container via the JTA UserTransaction interface, the session will hook to it

In both cases, the Transaction object returned by session.beginTransaction() should deal with the underlying JTA Transaction (commits, rollbacks).

What's still not clear to me:
- if the JTA transaction has been started because of a call to openSession(), will it be closed when calling: a) session.close() or b) tx.commit()?
of tx.commit()? close/commit the JTA transaction? (I understand that directly or indirectly calling UserTransaction.commit() will trigger some "commit callbacks" in the attached Hibernate session and transaction)
- if the session (re)used the JTA transaction in progress, what is the effect

What I will do is, as suggested in section 11.1.1 and by some user in this forum, is:
- use a filter to manage session.openSession() and session.close()
- use BMT for EJBs session facades that will call session.openSession(), then business/objects methods, and session.close()
- use session.getCurrentSession() anywhere else in the code

I would suggest having a table or two in the documentation explaining how the instances of UserTransaction, Session and Transaction interact in a jta environment. The tables could explain the effects of calling methods like session.openSession(), session.currentSession(), session.close(), transaction.commit(), UserTransaction.commit(), and this, if a JTA Transaction is in progress or not or if it has been started by Hibernate....etc. I thing this should be of great help (At least for me..)

And BTW Congratulations to everybody implied with the design/development of Hibernate. This product is fantastic..Thanks a lot


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.