-
1. Re: TransactionException: could not register synchronization
holmes.j Sep 13, 2006 6:32 PM (in response to holmes.j)I guess I forgot some slightly useful information.
JBoss 4.04GA
Hibernate 3.2CR2
Hibernate Annotations 3.2.0CR1 -
2. Re: TransactionException: could not register synchronization
holmes.j Sep 13, 2006 8:00 PM (in response to holmes.j)So the design I'm going for is something like the one described in this thread http://forum.hibernate.org/viewtopic.php?t=927886 or this one http://www.hibernate.org/328.html.
It's where the Business objects have no clue where or how the objects come from, they just talk to a Dao, and are merry.
So I guess the problem is that I wasn't starting a Transaction anywhere. I blindly assumed that automagically Hibernate would create a Transaction object during the getCurrentSession(), if it was setup to use JTA.
Obviously that's not the case, and the answer is to use AOP to invoke a session like this http://www.hibernate.org/391.html. Never played with AOP before, but that's a different discussion.
Does this sound right?
-Jason -
3. Re: TransactionException: could not register synchronization
holmes.j Sep 14, 2006 1:37 PM (in response to holmes.j)I'm going crazy trying to figure this out.
So I was reading the Hibernate docs again and came across this ...
With CMT, transaction demarcation is done in session bean deployment descriptors, not programatically, hence, the code is reduced to:
// CMT idiom
Session sess = factory.getCurrentSession();
// do some work
...
In a CMT/EJB even rollback happens automatically, since an unhandled RuntimeException thrown by a session bean method tells the container to set the global transaction to rollback. This means you do not need to use the Hibernate Transaction API at all with BMT or CMT, and you get automatic propagation of the "current" Session bound to the transaction.
So I was write, it is supposed to automagically create the Transaction. I was stepping through my session bean, and it looks like it did create a transaction, it's just that it hasn't started the transaction. This then throws the HibernateException because the transaction is not in a valid state.
Anybody know how to use EJB Session Beans w/ Hibernate, JTA and all this wonderful stuff? -
4. Re: TransactionException: could not register synchronization
holmes.j Sep 18, 2006 6:40 PM (in response to holmes.j)In the hope that somebody can help ...
I have a EJB 2.1 Stateless Session Bean with this header .../** * @ejb.bean name="GeoFeatureServer" * jndi-name="com.free2be.framework.gis.feature.GeoFeatureServerHome" * type="Stateless" view-type="remote" * @ejb.transaction type="Required" */
, that does this ...
/** * @ejb.interface-method */ public GeoFeature findGeoFeature(GeoFeaturePk geoFeaturePk) throws NoSuchElementException { GeoFeature value = (GeoFeature) cache.getByKey(geoFeaturePk); if (value == null) { getLogger().debug(geoFeaturePk + " not cached...loading"); GeoFeature featureValue = getGeoFeatureDao().findById(geoFeaturePk, false); value = new GeoFeature(featureValue); cache.addToCache(value); } return value; }
and my Dao looks like this ...@SuppressWarnings("unchecked") public V findById(long id, boolean lock) { V entity; try { Session session = getSession(); // Transaction tx = session.beginTransaction(); if (lock) entity = (V) session.load(getPersistentClass(), id, LockMode.UPGRADE); else entity = (V) session.load(getPersistentClass(), id); } catch (HibernateException e) { throw new NoSuchElementException(e.getMessage()); } return entity; }
If I take out the session.beginTransaction, I get the "load is not valid without active transaction." If I leave it in, it works like it should.
Any ideas as to what/how I'm supposed to configure to get the Application Server to do what it's supposed to do?
-Jason