Hibernate 4.3 + Tomcat 7无法查找JNDI名称 [英] Hibernate 4.3 + Tomcat 7 Unable to lookup JNDI name

查看:199
本文介绍了Hibernate 4.3 + Tomcat 7无法查找JNDI名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从4.2.7升级到Hibrernate 4.3.x时遇到问题。我得到这个异常:

$ p $ 引起:javax.naming.NameNotFoundException:Name [java:comp / env / jdbc / data]在这种情况下不受约束。无法找到[java:comp]。
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at javax.naming .InitialContext.lookup(InitialContext.java:415)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)
... 82 more

我使用的是Tomcat 7.0.29(试用7.0.47)和JDK 7(v25)。 Hibernate 4.2.7没有问题。



这是我的persistence.xml:

 < persistence-unit name =datatransaction-type =RESOURCE_LOCAL> 
java:comp / env / jdbc / data< / non-jta-data-source>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =hibernate.show_sqlvalue =false/>
< property name =hibernate.format_sqlvalue =true/>
< / properties>
< / persistence-unit>

但我认为问题不在这里。我做了一些调试研究,发现了以下内容:
$ b $ p $ j code> JndiServiceImpl #location(String jndiName)创建不同的初始上下文。

4.3.0 - org.apache.naming.NamingContext



4.2.7 - org.apache.naming.SelectorContext



为此,我找不到任何分歧。



我发现了一些类似的话题,但没有任何帮助。感谢您的帮助。

解决方案

是的,它的实际错误,因为他们对 session-factory 工作时以及何时请求工厂连接。



所以你需要在后面解析它,会话工厂会话工厂请求重新连接时使用它。



尝试通过旧scool hibernate.cfg.xml 像这样解决jndi中的DataSource的问题:

 < hibernate-configuration> 
< session-factory name =data>
< property name =connection.datasource> java:comp / env / jdbc / data< / property>
...

然后通过 persistence.xml解析配置像这样:

 < persistence version =2.0> 
< persistence-unit name =data>
<属性>
< property name =hibernate.ejb.cfgfilevalue =hibernate.cfg.xml/>

也许您的 hbm2ddl 必须移至 hibernate.cfg.xml 也是。



玩得开心。


I have a trouble upgrading to Hibrernate 4.3.x from 4.2.7. I got this exception:

Caused by: javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/data] is not bound in this Context. Unable to find [java:comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at javax.naming.InitialContext.lookup(InitialContext.java:415)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)
... 82 more

I'm using Tomcat 7.0.29 (tried 7.0.47 as well) and JDK 7 (v25). There is no problem with Hibernate 4.2.7.

Here is my persistence.xml:

<persistence-unit name="data" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>java:comp/env/jdbc/data</non-jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
    </properties>
</persistence-unit>

But I don't think the problem is here. I did a little bit of debug research and found out the following:

JndiServiceImpl#locate(String jndiName) creates an initial context which differs.

4.3.0 - org.apache.naming.NamingContext

4.2.7 - org.apache.naming.SelectorContext

Except for this, I couldn't find any more differences.

I found some similar topics for this issue, but none of help. Thanks for any help.

解决方案

Yea, its actually buggy, because they took different focus on how a session-factory works and when the factory is asked for a connection.

So you need to resolve it behind the session-factory to use it when the session-factory requests a fresh connection.

Try to solve the DataSource in jndi in sole respolsibility of hibernate, via the old-scool hibernate.cfg.xml like this:

<hibernate-configuration>
    <session-factory name="data">
        <property name="connection.datasource">java:comp/env/jdbc/data</property>
    ...

And resolve the config via persistence.xml like this:

<persistence version="2.0">
    <persistence-unit name="data">
        <properties>
            <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>

Maybe your hbm2ddl must move to hibernate.cfg.xml too then.

Have fun.

这篇关于Hibernate 4.3 + Tomcat 7无法查找JNDI名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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