mysql – 无法在Tomcat7中创建资源实例

我正在尝试在Tomcat 7中配置连接池.这是代码:
server.xml的一部分:

<GlobalNamingResources>
<!-- Editable user database that can also be used by
     UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />


<Resource  
name="jdbc/testDataSource"                                  
auth="Container"                                            
type="javax.sql.DataSource"                                   
driverClassName="com.mysql.jdbc.Driver"                      
url="jdbc:mysql://localhost:3306/delta_server"                
username="test" password="test"                              
validationQuery="select count (*) from session_contexts"              
/>

web.xml配置:

<resource-ref> 
    <description> 
        Sample JNDI DataSource resource reference 
    </description> 
    <res-ref-name>jdbc/testDataSource</res-ref-name>                   
    <res-type>java.sql.DatSource</res-type>                           
    <res-auth>Container</res-auth>                                     
</resource-ref> 

并从jsp页面访问:

        Context initialContext = new InitialContext();
        Context envContext = (Context) initialContext.lookup("java:/comp/env");
        conn = (Connection)  envContext.lookup("jdbc/testDataSource");

但不幸的是我得到了例外:

javax.naming.NamingException: Cannot create resource instance
org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146)
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
org.apache.naming.NamingContext.lookup(NamingContext.java:826)
org.apache.naming.NamingContext.lookup(NamingContext.java:145)
org.apache.naming.NamingContext.lookup(NamingContext.java:814)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.jsp.index_jsp._jspService(index_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我该如何解决?
谢谢.

解决方法:

您的配置看起来非常好(除了您的web.xml文件中的一些拼写错误;< res-type> java.sql.DatSource< / res-type>应该是< res-type> javax.sql.DataSource< / RES型&GT). 但我认为问题在于您在server.xml文件中声明数据库资源. 通常,应用程序资源应在应用程序的context.xml文件中声明,并且只有在应用程序之间共享时才在server.xml中声明.所以我的建议是在context.xml文件中声明jdbc / testDataSource资源.这应该是使其发挥作用的一种方式. 如果您必须绝对拥有全局资源,请在context.xml文件you must add a resource link to it or it won’t be visible by default中.

This context is distinct from the per-web-application JNDI contexts described in the JNDI Resources HOW-TO. The resources defined in this element are not visible in the per-web-application contexts unless you explicitly link them with <ResourceLink> elements.

因此,使其工作的第二种方法是将资源保留在server.xml中,但随后在context.xml文件中添加如下内容:

<ResourceLink global="jdbc/testDataSource" 
              name="jdbc/testDataSource" 
              type="javax.sql.DataSource" />
上一篇:java – 我在哪里可以得到(匹配)com.sun.jndi.ldap.*包的源代码?


下一篇:Maven配置文件,Spring,JDBC和JNDI数据源