【问题标题】:Cannot create resource instance in Tomcat7无法在 Tomcat7 中创建资源实例
【发布时间】:2012-07-15 04:18:03
【问题描述】:

我正在尝试在 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)

我该如何解决? 谢谢你。

【问题讨论】:

    标签: mysql jndi connection-pooling tomcat7 server.xml


    【解决方案1】:

    您的配置看起来相当不错(除了您的 web.xml 文件中的一些拼写错误;&lt;res-type&gt;java.sql.DatSource&lt;/res-type&gt; 应该是 &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&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 中。

    此上下文不同于 JNDI 资源 HOW-TO 中描述的每个 Web 应用程序的 JNDI 上下文。此元素中定义的资源在每个 Web 应用程序上下文中不可见,除非您将它们与 元素显式链接。

    所以使其工作的第二种方法是保留在server.xml 中声明的资源,然后在您的context.xml 文件中添加类似这样的内容:

    <ResourceLink global="jdbc/testDataSource" 
                  name="jdbc/testDataSource" 
                  type="javax.sql.DataSource" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      相关资源
      最近更新 更多