spring 带有 Tomcat 6 和 Eclipse 的 JNDI 数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6344935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:32:59  来源:igfitidea点击:

JNDI DataSource with Tomcat 6 and Eclipse

eclipsespringtomcatjndi

提问by Joaquín L. Robles

I can't get my DataSource working with JNDI and Tomcat 6, while running it from Eclipse. I've added a context.xml to my /META-INF with the following content:

从 Eclipse 运行它时,我无法让我的 DataSource 与 JNDI 和 Tomcat 6 一起工作。我已将 context.xml 添加到我的 /META-INF 中,内容如下:

<Context>

<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"
     username="root"
     password="root"
     driverClassName="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/database"
     maxActive="15"
     maxIdle="7"
     validationQuery="Select 1" />
</Context>

And configured my Spring Bean as follows:

并按如下方式配置我的 Spring Bean:

<bean id="UserDatabase" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/myDB"></property>
    <property name="lookupOnStartup" value="true"></property>
    <property name="cache" value="true"></property>
    <property name="proxyInterface" value="javax.sql.DataSource"></property>
</bean>

I've also added this lines to my web.xml:

我还在我的 web.xml 中添加了这一行:

<resource-ref>
    <description>Connection Pool</description>
    <res-ref-name>jdbc/myDB</res-ref-name>
    <res-type>javax.sql.Datasource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

But for some reason I still get this error:

但出于某种原因,我仍然收到此错误:

javax.naming.NameNotFoundException: The name jdbc is not associated to this context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)

I can't understand why this is not working... Any idea?

我不明白为什么这不起作用......知道吗?

采纳答案by Joaquín L. Robles

I changed the following and now it works:

我更改了以下内容,现在可以使用了:

In my context.xml completed the Contexttag with:

在我的 context.xml 中完成了Context标记:

<Context docBase="myApp" path="/myApp" reloadable="true" source="org.eclipse.jst.jee.server:app">

And in the Connection URL the character &caused the Cannot create resourceerror, don't know why, so my URL now is like:

在连接 URL 中,字符&导致Cannot create resource错误,不知道为什么,所以我的 URL 现在是这样的:

jdbc:mysql://localhost/database?useUnicode=true&amp;characterEncoding=utf-8

Please note the &amp;int the URL...

请注意&amp;网址中的int ...

回答by danny.lesnik

If I remember correctly you should access it as

如果我没记错的话你应该访问它

<property name="jndiName" value="java:comp/env/jdbc/myDB"/>

回答by abalogh

In the Spring appcontext, replace your definition with:

在 Spring appcontext 中,将您的定义替换为:

<bean id="dataSource" 
      class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" 
                  value="java:comp/env/jdbc/myDB"/>    
      <property name="resourceRef" 
                  value="true" /> 
</bean>