javax.naming.NameNotFoundException: Name [jdbc/spitterDS] 在此上下文中未绑定。找不到 [jdbc]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21934989/
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
javax.naming.NameNotFoundException: Name [jdbc/spitterDS] is not bound in this Context. Unable to find [jdbc]
提问by user3337218
I am trying to access a JNDI datasource
in tomcat using Spring's jee jndi-lookup
tag.
我正在尝试datasource
使用 Spring 的jee jndi-lookup
标记访问tomcat 中的 JNDI 。
The exception is indicating that I haven't registered my datasource
correctly but I'm unable to figure out why not.
异常表明我没有datasource
正确注册,但我无法弄清楚为什么不正确。
Here is my code:-
这是我的代码:-
service-context.xml:-
服务上下文.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">
<context:component-scan base-package="spitter" />
<mvc:annotation-driven />
<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitterDS" resource-ref="true" />
</beans>
webapp/META-INF/context.xml:-
webapp/META-INF/context.xml:-
<Context path="/spitter" reloadable="true" cachingAllowed="false" antiResourceLocking="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/spitterDS" auth="Container" type="org.apache.commons.dbcp.BasicDataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="password"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/spitter" />
</Context>
web.xml:-
web.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Spitter</display-name>
<resource-ref>
<description>Spitter DS</description>
<res-ref-name>jdbc/spitterDS</res-ref-name>
<res-type>org.apache.commons.dbcp.BasicDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
and finally,
最后,
MainController.java:-
主控制器.java:-
package spitter.mvc;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class MainController {
@Resource(name="dataSource")
private DataSource dataSource;
@RequestMapping(method = RequestMethod.GET)
public String showHome(Model model) throws SQLException{
Connection con = dataSource.getConnection();
return "spitter";
}
}
I am using Tomcat 7
in Eclipse kepler
for JEE developers. The exception when I start the tomcat server in eclipse:-
我使用Tomcat 7
的Eclipse kepler
为JEE开发。我在 eclipse 中启动 tomcat 服务器时的异常:-
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spitterDS] is not bound in this Context. Unable to find [jdbc].
org.springframework.beans.factory.BeanCreationException:创建名为“mainController”的bean时出错:资源依赖注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“dataSource”的 bean 时出错:调用 init 方法失败;嵌套异常是 javax.naming.NameNotFoundException: Name [jdbc/spitterDS] 在此上下文中未绑定。无法找到 [jdbc]。
I assume I have made a typo somewhere but cannot find it. Please help!
我想我在某处打错了但找不到。请帮忙!
回答by Ravinder Reddy
"/jdbc/spitterDS"
is not equal to "jdbc/spitterDS"
.
"/jdbc/spitterDS"
不等于"jdbc/spitterDS"
。
In service-context.xml
file,
Change
在service-context.xml
文件中,
更改
<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitterDS" resource-ref="true" />
To
到
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/spitterDS" resource-ref="true" />
Edit:
编辑:
Error creating bean with name 'dataSource':
创建名为“dataSource”的 bean 时出错:
As per documentation:
根据文档:
... in the
@Resource(name="jdbc/Foo") DataSource ds;
annotation, the global JNDI name isjdbc/Foo
.
...
The @Resource annotation in the application code looks like this:@Resource(name="jdbc/helloDbDs") javax.sql.DataSource ds;
... 在
@Resource(name="jdbc/Foo") DataSource ds;
注释中,全局 JNDI 名称是jdbc/Foo
.
...
应用程序代码中的 @Resource 注释如下所示:@Resource(name="jdbc/helloDbDs") javax.sql.DataSource ds;
Change:
改变:
@Resource(name="dataSource")
private DataSource dataSource;
To:
到:
@Resource(name="jdbc/spitterDS")
private DataSource dataSource;
Refer To: Using the Java Naming and Directory Interface.
请参阅:使用 Java 命名和目录接口。
回答by Aniket Thakur
I got same exact error. But for me following was missing from Resource
我得到了同样的错误。但对我来说,资源中缺少以下内容
driverClassName="oracle.jdbc.OracleDriver"
After adding that, everything worked perfectly.
添加后,一切正常。
回答by Vielinko
I faced off this problem too, in my case I missed a WARN from tomcat log, I didn't include the jar for
我也遇到了这个问题,就我而言,我错过了来自 tomcat 日志的警告,我没有包含 jar
org.apache.commons.dbcp.BasicDataSourceFactory
commons-dbcp-x.x.jar
commons-pool-x.x.x.jar
commons-lang-x.x.x.jar
Fixed this, all runs fine.
解决了这个问题,一切正常。
回答by Tim Pizey
In my case I had to increase the setting in my.cnf
max_connections = 120
I get the impression that any misconfiguration of mysql will give this error message.
就我而言,我不得不增加 my.cnf 中的设置,
max_connections = 120
我的印象是 mysql 的任何错误配置都会给出此错误消息。