Hi,
I want to synchronize the hibernate session with transactions in a JTA environment of an ejb container (WebLogi 8.1) transparently as described at
http://www.hibernate.org/245.html "Synchronizing Hibernate3 with JTA".
The session synchronization works fine unless I place 'hibernated' code inside an ejbCreate-method of a Session-Bean. Whenever I do this, no changes are commited to the database.
Let me show an example: (from Main.java)
Code:
public static void main(String[] args) throws Exception {
// First try
{
Long id = new Long( 50000);
SampleSession session = SampleSessionUtil.getHome().create();
session.createMccAgent( id, "dummy2", null);
session.remove();
// This worked fine. I can find a new row with ID=50000 inside the DB
}
// Alternative try
{
Long id = new Long( 50001);
SampleSession session = SampleSessionUtil.getHome().create( id, "dummy3", null);
session.remove();
// This didn't work. No new row has been inserted.
}
}
This is the server-side code: (from SampleSessionBean.java)
Code:
/**
* @ejb.bean
* type="Stateful"
* view-type="remote"
* name="SampleSession"
* jndi-name="hsampel/session/SampleSession"
*
* @ejb.transaction
* type="Required"
*
*
* @ejb.ejb-ref
* ejb-name="SampleSession" view-type="remote"
*/
public class SampleSessionBean extends AbstractRemoteSessionBean implements SessionBean {
/**
* @ejb.create-method
*/
public void ejbCreate() throws CreateException {
}
/**
* @ejb.create-method
*/
public void ejbCreate( Long id, String ntName, Long mccId) throws CreateException {
this.createMccAgent( id, ntName, mccId);
}
/**
* @ejb.interface-method
*/
public void createMccAgent( Long id, String ntName, Long mccId) {
Session session = null;
try {
session = HibernateUtil.instance().getSession();
MccAgent agent = new MccAgent( id, ntName, mccId);
session.save( agent);
} catch ( HibernateException ex ) {
throw new EJBException( ex);
}
}
}
As you can see, this code does not contain any call to session.flush() or session.close(), since this should be done by Hibernate3 itself when the current transaction is being commited by the EJB container. Actually this happens, when method createMccAgent is called, but not, when method ejbCreate( Long id, String ntName, Long mccId) is called.
For me it looks like the WebLogic TransactionManager does not expose the current Transaction when called within an ejbCreate method, but I am not sure - perhaps I did misconfigure something.
Is this true? If so, what can I do?
Any help would really be appreciated.
Thanks for your time.
Michael
hibernate.cfg.xml: Code:
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">jdbc/HibernateSampleDS</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="show_sql">true</property>
<property name="cache.use_query_cache">false</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.query.substitutions">true 1, false 0</property>
<mapping resource="mapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate version: 3.0
Mapping documents:Code:
<hibernate-mapping>
<class name="de.sanacorp.sample.server.interfaces.MccAgent" table="MCC_AGENT">
<id column="ID" name="id" type="long">
<generator class="assigned">
</generator>
</id>
<property column="NT_NAME" name="ntName" type="java.lang.String"></property>
<property column="MCC_ID" name="mccId" type="java.lang.Long"></property>
</class>
</hibernate-mapping>
Name and version of the database:
Oracle 8.1