jpa - Hibernate throws NamingException looking up my datasource into liferay context -
i've liferay (6.1.0 bundled tomcat 7.0.23) portlet use own database different liferay db.
well, if access db tomcat connection pooling works
string jndiname = "java:/comp/env/jdbc/mydb"; try { context ctxt = new initialcontext(); name name = ctxt.getnameparser( "" ).parse( jndiname); datasource ds = (datasource) ctxt.lookup( name ); //i've got datasource } catch (namingexception e) { system.out.println("[namingexception]"); }
if use jpa (implemented hibernate 4), throws namingexception. persistence.xml
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="mypu"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <non-jta-data-source>java:/comp/env/jdbc/mydb</non-jta-data-source> <properties> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.mysqldialect" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit>
this (simple) code
entitymanagerfactory emf=persistence.createentitymanagerfactory("mypu"); entitymanager em=emf.createentitymanager();
and relevant part of stacktrace
caused by: org.hibernate.engine.jndi.jndiexception: unable lookup jndi name [java:/comp/env/jdbc/mydb] @ org.hibernate.engine.jndi.internal.jndiserviceimpl.locate(jndiserviceimpl.java:117) @ org.hibernate.engine.jdbc.connections.internal.datasourceconnectionproviderimpl.configure(datasourceconnectionproviderimpl.java:115) @ org.hibernate.boot.registry.internal.standardserviceregistryimpl.configureservice(standardserviceregistryimpl.java:111).
well, downloaded hibernate sources , piece of code of class org.hibernate.engine.jndi.internal.jndiserviceimpl
public object locate(string jndiname) { final initialcontext initialcontext = buildinitialcontext(); final name name = parsename( jndiname, initialcontext ); try { return initialcontext.lookup( name ); } catch ( namingexception e ) { throw new jndiexception( "unable lookup jndi name [" + jndiname + "]", e ); } { cleanup( initialcontext ); } }
where buildinitialcontext() new initialcontext()
so, seems hibernate , me same thing matter initialcontext different ad lookup hibernate fails.
be in mind context defined under tomcat_home/catalina/localhost. in mind if in persistence.xml don't use tomcat datasource put connection parameters...it works!
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb" /> <property name="javax.persistence.jdbc.user" value="myuser" /> <property name="javax.persistence.jdbc.password" value="mypwd" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.driver" />
thanks in advance
Comments
Post a Comment