Hi
I am using JNDI to access the SessionFactory. My question is, do I
still need to put the creation of the SessionFactory inside a
HibernateUtil class like on page 297, or is it enough to the use the JNDI?
My code looks like this without using HibernateUtil. In this way, is the
Sessionfactory initiliazed just once without using HibernateUtil?
Accordingly, do I in my case need to use a HibernateUtil class?
// Create session
SessionFactory sessionFactory = null;
try {
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:/hibernate/HibernateFactory");
sessionFactory = (SessionFactory) obj;
} catch (NamingException e) {
throw new DataAccessException("Error looking up HibernateFactory
through JNDI");
}
Session session = sessionFactory.openSession();
// Perform transaction
Transaction customerTransaction = null;
try {
customerTransaction = session.beginTransaction();
session.save(customer);
customerTransaction.commit();
} catch (HibernateException e) {
customerTransaction.rollback();
throw new DBTransactionException("Transaction error. Transaction
rolled back");
} finally {
session.flush();
session.close();
}
|