使用lookUp可以使JNDI查找正常工作,但不能在JPA的persistence.xml中使用时 [英] JNDI lookup works fine using lookUp but not when used in persistence.xml of JPA

查看:105
本文介绍了使用lookUp可以使JNDI查找正常工作,但不能在JPA的persistence.xml中使用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tomcat连接池,jpa,休眠模式.如果我尝试使用:p来获取它,我在tomcat的context.xml中创建的数据源可以正常工作.

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");

但是如果我在persistence.xml中指定此jndi数据源

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>

我收到以下异常: org.hibernate.service.jndi.JndiException:无法查找JNDI名称[jdbc/kids]

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

任何想法为什么会发生!

Any idea why it could happen !

推荐答案

最后,在我指定了以下属性后,它终于在今天工作了.我们需要指定一些属性,例如要使用的方言.如果我们指定数据源;我们不需要指定用户名,数据库的密码url(因为所有这些都是在数据源配置本身中指定的).

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) .

最重要的一点是指定数据源的方式.它应该是完整的路径:java:/comp/env/jdbc/kids. 所有这些,而我在comp之前就缺少斜杠.

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>

这篇关于使用lookUp可以使JNDI查找正常工作,但不能在JPA的persistence.xml中使用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆