I had this problem Monday but, at that time, I knew NOTHING about JNDI. It turns out that for Tomcat (I'm on v4.1.24) the Context name they gave is read-only: "java:/comp/env". It took me a few hours before I got the brains (read: duh!) to try another context. I made up the name:
"dgf:/hibernate/SessionFactory" such as:
<session-factory name="dgf:/hibernate/SessionFactory">
Wouldn't you know it, Hibernate has the brains to create that context for you and store itself in there. I accessed it with this code snippet:
==========================================
Context ctx;
SessionFactory sf;
try {
ctx = new InitialContext();
ctx = (Context) ctx.lookup("dgf:/hibernate");
sf = (SessionFactory) ctx.lookup("SessionFactory");
} catch (
} catch (NamingException ne) {
log.info("Y1, Context exception: " + ne.getMessage());
} catch (HibernateException he) {
log.info("Y2: Hibernate exception: " + he.getMessage());
}
==========================================
Why did I do this? So I could create a facade for Hibernate 2.0.3 under Struts 1.1 without passing references to the Session, SessionFactory, or the request or application context back and forth. Now, I just create the object, have it reconnect before the transaction, disconnect after the transaction, and never need to hand it the Session, SessionFactory, request, or servletContext. It's a bit more convenient for me this way to just create an SQL class object and say sql.load(whatever).
Regards,
David
|