hibernate - java.lang.IllegalStateException: Could not locate SessionFactory in JNDI -
good evening, above exception when using hibernate jsf, saw many times in past , root cause <session-factory name="sessionfactory">
removed name , change generated code creating sessionfactory that:
protected sessionfactory getsessionfactory() { try { return (sessionfactory) new initialcontext() .lookup("sessionfactory"); } catch (exception e) { log.error("could not locate sessionfactory in jndi", e); throw new illegalstateexception( "could not locate sessionfactory in jndi"); } }
to that:
protected sessionfactory getsessionfactory() { try { return new configuration().configure("hibernate.cfg.xml").buildsessionfactory(); } catch (exception e) { log.error("could not locate sessionfactory in jndi", e); throw new illegalstateexception( "could not locate sessionfactory in jndi"); } }
it working fine me, time have no solution it, know problem resides?
the hibernate-cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/guno</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> <property name="hibernate.search.autoregister_listeners">false</property> </session-factory>
you manually add sessionfactory context. although looks lot of code these 5 lines. rest handeling namingexception initialcontext seems love throwing.
a better approach use contextlistener automatically add session during start up
initialcontext initialcontext = new initialcontext(); sessionfactory sf = (sessionfactory) initialcontext.lookup("sessionfactory"); configuration cfg = new configuration(); cfg.configure(); sf = cfg.buildsessionfactory(); initialcontext.bind("sessionfactory", sf);
here full servlet goget method
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { account acc; initialcontext initialcontext = null; acc = new account("asdf" + string.valueof(new date().gettime()), "asdf"); accounthome home; transaction tx = null; sessionfactory sf; // create instance of initialcontext // can lookup sessionfactory property // or add if not yet exist try { initialcontext = new initialcontext(); } catch (namingexception e) { throw new servletexception("unable create initalcontext", e); } // since sessionfactories expensive create // first attempt lookup cached instance of sessionfactory try { sf = (sessionfactory) initialcontext.lookup("sessionfactory"); } catch (namingexception e) { // there no session factory bound context // manually create , bind configuration cfg; cfg = new configuration(); cfg.configure(); sf = cfg.buildsessionfactory(); try { initialcontext.bind("sessionfactory", sf); } catch (namingexception e1) { throw new servletexception( "unable bind sessionfactory inital context"); } } // start transaction , perform work tx = sf.getcurrentsession().begintransaction(); try { home = new accounthome(); home.persist(acc); tx.commit(); } catch (exception e) { tx.rollback(); throw new servletexception("work failed", e); } }
edit: added contextlistener
package ch.yaawi.platform; import javax.naming.initialcontext; import javax.naming.namingexception; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; public class sessionfactorylistener implements servletcontextlistener { private sessionfactory msessionfactory; public void contextdestroyed(servletcontextevent event) { if (msessionfactory != null && !msessionfactory.isclosed()) { msessionfactory.close(); } } public void contextinitialized(servletcontextevent event) { initialcontext initialcontext = null; try { initialcontext = new initialcontext(); } catch (namingexception e) { throw new runtimeexception("unable create initalcontext", e); } try { msessionfactory = (sessionfactory) initialcontext .lookup("sessionfactory"); } catch (namingexception e) { configuration cfg; cfg = new configuration(); cfg.configure(); msessionfactory = cfg.buildsessionfactory(); try { initialcontext.bind("sessionfactory", msessionfactory); } catch (namingexception e1) { throw new runtimeexception( "unable bind sessionfactory inital context"); } } } }
then in web.xml
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>archetype created web application</display-name> <listener> <listener-class>ch.yaawi.platform.sessionfactorylistener</listener-class> </listener> </web-app>
of causing changing namespace own.
hope helps someone
Comments
Post a Comment