Tomcat - Mysql -Persistence...无法创建资源

Posted

技术标签:

【中文标题】Tomcat - Mysql -Persistence...无法创建资源【英文标题】:Tomcat - Mysql -Persistence... Cannot create resource 【发布时间】:2014-08-02 13:15:30 【问题描述】:

我有一个在 Glassfish + postgreSQl + JPA 中运行的 webapp,我应该将它迁移到 Tomcat + mysql。 我在 MYsql 中有可用的数据,但我面临资源创建问题。

Maven 项目 --> META-INF --> context.xml

<Resource auth="Container" 
          driverClassName="com.mysql.jdbc.Driver" 
          maxActive="10" maxIdle="4" 
          name="jdbc/pool" 
          password="pwd" 
          type="javax.sql.DataSource" 
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          url="jdbc:mysql://xx.xx.xx.xx:3306/db" 
          username="user"/>

项目 --> src/main/resouces/META-INF --> persistence.xml

<persistence-unit name="testPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/Pool</jta-data-source>        
    <class> </class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://xx.xx.xx.xx:3306/db?zeroDateTimeBehavior=convertToNull"/>
        <property name="javax.persistence.jdbc.password" value="pwd"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.user" value="test"/>
        <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
</persistence-unit>

Maven 项目 --> WEB-INF --> web.xml

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

我有 mysql-connector undet tomcat/lib

部署时,tomcat日志有

INFO: Deploying configuration descriptor F:\Dev_Env\apache-tomcat-7.0.42\conf\Catalina\localhost\TestApp.xml
Jun 12, 2014 2:37:37 PM org.apache.tomcat.jdbc.pool.ConnectionPool init

但是没有创建资源实例

SEVERE: Servlet /TestApp threw load() exception
    javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource(DefaultInstanceManager.java:580)
    at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:470)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:157)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:138)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:535)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1461)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1445)
    at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:860)
    at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:357)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

【问题讨论】:

【参考方案1】:

Tomcat 并不完全支持 Java EE,它只是一个 Servlet 容器(实现了 Servlet 和 JSP 规范) What is the difference between Tomcat, JBoss and Glassfish?)

如果您的应用程序正在使用 Java EE 资源(例如 EJB),您应该无法使用 Tomcat 运行它。

我建议使用 TomCat 从头开始​​创建一个新应用程序,然后检查它是否运行,然后您可以比较配置文件以确保 Glassfish 配置中的任何内容都没有留下。

【讨论】:

我从头开始创建了一个应用程序,我有两个持久性单元,一个 RESOURCE_LOCAL 和另一个 JPA。我还在应用程序 context.xml 中配置了资源。但即使这样,它也说无法创建资源实例。我使用 eclipselink jpa 提供程序。【参考方案2】:

对于你应该使用的 tomcat,transaction_type 应该是“RESOURCE_LOCAL”

【讨论】:

以上是关于Tomcat - Mysql -Persistence...无法创建资源的主要内容,如果未能解决你的问题,请参考以下文章

大话tomcat之tomcat连接mysql

Linux集成环境部署 mysql + tomcat + redis

Tomcat登陆mysql的密码设置

Tomcat - Mysql -Persistence...无法创建资源

mysql tomcat redis nginx 版本的查看方法

tomcat+jsp+mysql 实现jsp页面连接mysql