I am using Hibernate 2.1.8 and JBoss 4.0.1. I was using the session facade pattern and everything was working fine. Since I have the Hibernate in Action book, I read chapter 8 and decided to implement the DAO and Command patterns.
JBoss gives the following ...
Code:
...
15:20:06,622 INFO [SessionFactoryObjectFactory] Factory name: java:/hibernate/SessionFactory
...
So when I used the session facade I hooked up to the session factory :
Code:
public void ejbCreate() throws CreateException {
try {
InitialContext ctx = new InitialContext();
_sessionFactory = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
} catch (NamingException ex) {
_ctx.setRollbackOnly();
throw new CreateException("ManagerBean(ejbCreate): Unable to get a Local Home interface. "+
"Probably a local ref or JNDI naming problem. "+ex.getMessage());
}
}
The session factory was found and used to get a session.
Now, I created a CommandHandlerBean and using HibernateUtil.getSession() in my DAO, i can't find the session factory.
Here is the HibernateUtil code :
Code:
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();
// Create the initial SessionFactory from the default configuration files
static {
try {
// we can build the session factory ...
//configuration = new Configuration();
//sessionFactory = configuration.configure().buildSessionFactory();
//System.out.println("session factory is "+sessionFactory);
// or We could also let Hibernate bind it to JNDI:
System.out.println("get session factory");
sessionFactory = getSessionFactory();
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
ex.printStackTrace();
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* Returns the SessionFactory used for this static class.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
// Instead of a static variable, use JNDI:
// /*
SessionFactory sessionF = null;
try {
Context ctx = new InitialContext();
System.out.println("looking up sf ...");
sessionF = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory");
} catch (NamingException ex) {
throw new InfrastructureException(ex);
}
return sessionF;
// */
// Instead of JNDI, use static variable (comment out above code)
//return sessionFactory;
}
Even though the JNDI names are exactly the same, now I get the following exception.
Code:
get session factory
looking up sf ...
com.adcware.buers.exception.InfrastructureException: javax.naming.NamingException: Name not found java:/hibernate/SessionFactory
at com.adcware.buers.util.HibernateUtil.getSessionFactory(HibernateUtil.java:70)
at com.adcware.buers.util.HibernateUtil.<clinit>(HibernateUtil.java:43)
at com.adware.buers.DAO.BranchDAO.<init>(BranchDAO.java:25)
at com.adcware.junit.test.Tester.testCreateBranch(Tester.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Does anyone have a clue as to why the same JNDI name that was found before now is not able to be found?
Many thanks and kind regards,
Joe