Hi,
I am trying to use JTA transactions in my application but the transaction don't commit. I've already tried to upgrade the hibernate to version 3.x with EJB in an ambient CMT with all the EJB's methods configured to Required but it got the same problem. Below are the files that I'm using now in JBOSS 3.2.1 and Hibernate 2.1.8 with CMT and all the methods still with Required.
hibernate.cfg.xml
<property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0</property>
<property name="cglib.use_reflection_optimizer">false</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="transaction.manager_lookup_class">net.sf.hibernate.transaction.JBossTransactionManagerLookup</property>
ConnectionFactory
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class ConnectionFactory {
private static ConnectionFactory instance = null;
private SessionFactory sessionFactory = null;
private ConnectionFactory() {
// Establish SessionFactory for Hibernate
try {
sessionFactory =
new Configuration().configure("/WEB-INF/hibernate.cfg.xml").buildSessionFactory();
} catch (MappingException e) {
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's
* request to fail.
*
*/
throw new SystemException(e);
} catch (HibernateException e) {
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's request to fail.
*
*/
throw new SystemException(e);
}
}
/**
* getInstance() returns the instance of the ConnectionFactory singleton.
*
* Example call to retrieve session:
*
* <pre>
* Session session = ConnectionFactory.getInstance().getSession();
* </pre>
*
* @return Instance of the <code>ConnectionFactory</code> singleton.
*/
public static synchronized ConnectionFactory getInstance() {
/*
* If the instance of the Singleton has not been created, create and
* return.
*/
if (instance == null) {
instance = new ConnectionFactory();
}
return instance;
}
/**
* getSession() returns a Hibernate <code>Session</code>
*
* @return <code>Session</code> retrieved from Hibernate <Code>SessionFactory</code>
*/
public Session getSession() {
try {
/*
* Use the Hibernate Session Factory to return an open session to the caller.
*/
Session s = sessionFactory.openSession();
return s;
} catch (HibernateException e) {
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's request to fail.
*
*/
throw new SystemException(e);
}
}
}
Code between sessionFactory.openSession() and session.close():
Session session = ConnectionFactory.getInstance().getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(entidade);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
} catch (Exception e2) {
e2.printStackTrace();
}
e.printStackTrace();
} finally {
if (session != null) {
try {
session.close();
} catch (HibernateException e) {
throw new SystemException(e);
}
}
}
|