JNDI lookup works fine using lookUp but not when used in persistence.xml of JPA
Asked Answered
R

2

14

I am using tomcat connection pool, jpa, hibernate. The datasource i created in context.xml of tomcat works fine if I try to get it using :

source = (DataSource) ((Context) c.lookup("java:comp/env")).lookup("jdbc/kids");

but if i specify this jndi datasource in persistence.xml

<persistence-unit name="kids-tomcat" transaction-type="JTA">
           <jta-data-source>jdbc/kids</jta-data-source>
       </persistence-unit>

I am getting following exception: org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [jdbc/kids]

Any idea why it could happen !

Ruckman answered 19/4, 2012 at 17:26 Comment(2)
If data-source name is kids, then you can try specifying java:kids in persistence.xmlSpellman
I tried java:jdbc/kids ; but then I got "This context must be accessed through a java: URL"Ruckman
R
17

Finally it worked today after i specified the properties below.. because just mentioning the datasource is not enough; we need to specify some properties like which dialect to use. If we specify datasource; we need not specify username , password url of the database ( as all are specified in the datasource configuration itself) .

Most important point is the way you specify the datasource. It should be complete path: java:/comp/env/jdbc/kids . All this while I was missing the slash just before comp.

<persistence-unit name="kids" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>java:/comp/env/jdbc/kids</non-jta-data-source>
    <class>com.kids.domain.User</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="connection.autocommit" value="false"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.show_sql" value="true"/>
    </properties>
</persistence-unit>

Ruckman answered 1/5, 2012 at 15:48 Comment(1)
All this while I was missing the slash just before comp.Ruckman
A
1

Maybe try the persistence.xml JNDI defined name including both the namespace ("java:comp/env") as well as the JNDI path ("jdbc/kids") in a single-string (i.e put them together).

Antaeus answered 19/4, 2012 at 18:11 Comment(2)
If i use java:comp/env/jdbc/kids; I get following exception:javax.naming.NamingException: This context must be accessed through a java: URLRuckman
Key point is the slash before comp is missing. java:/comp/env/jdbc/kids works fineRuckman

© 2022 - 2024 — McMap. All rights reserved.