Hi,
I'm using JBos + Hibernate. I want to know if it is possible to setup hibernate to automatically enroll the session in a container managed transaction. I have tried to set the <transaction-type> attribute in my ejb.jar to Container and comment out the session.beginTransaction() and tx.commit() but in this case no object goes into the DB.
Thanks.
========================
This is my code:
protected Session openNewSession()
throws MappingException, HibernateException, NamingException {
if (null == sessionFactory) {
InitialContext ctx = new InitialContext();
sessionFactory =
(SessionFactory) ctx.lookup(
"java:/hibernate/mfm/HibernateFactory");
}
Session session = sessionFactory.openSession();
return session;
}
Session session = null;
Device aDevice = null;
Transaction tx = null;
try {
session = openNewSession();
tx = session.beginTransaction();
aDevice = new Device(hardId, os, name);
session.save(aDevice);
tx.commit();
} catch (Exception hEx) {
hEx.printStackTrace();
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e) {
throw new EJBException(e);
}
throw new EJBException(hEx);
} finally {
try {
if (null != session)
session.close();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
return aDevice;
}
|