Mysql/Glassfish 资源 jdbc 无效

Posted

技术标签:

【中文标题】Mysql/Glassfish 资源 jdbc 无效【英文标题】:Mysql/Glassfish Invalid resource jdbc 【发布时间】:2016-03-24 17:09:51 【问题描述】:

我使用 Glassfish 4.1.1(构建 1)、JPA、EJB、Netbeans、EclipseLink 和 mysql。所以我有一个带有 JDBC 连接池和 JDBC 资源的 Web 应用程序项目。

这是我的 persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
        <persistence-unit name="JavaProjectPU" transaction-type="JTA">
        <jta-data-source>java:app/jdbc/JavaProjectRessource</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties/>
    </persistence-unit>
</persistence>

还有我的 glassfish-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/JavaProjectRessource" object-type="user" pool-name="connectionPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="connectionPool" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.DataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
    <property name="URL" value="jdbc:mysql://localhost:3306/javaproject"/>
    <property name="User" value="root"/>
    <property name="Password" value=""/>
  </jdbc-connection-pool>
</resources>

所以应用程序和 MySQL 数据库之间的所有配置似乎都很好,但是当我运行应用程序时,我得到了以下错误

Grave:   java.lang.RuntimeException: Invalid resource :  ResourceInfo : (jndiName=java:app/jdbc/JavaProjectRessource__pm), (applicationName=JavaProject) 
at com.sun.enterprise.connectors.ConnectorRuntime.lookupDataSourceInDAS(ConnectorRuntime.java:593)
at com.sun.enterprise.connectors.ConnectorRuntime.lookupPMResource(ConnectorRuntime.java:517)
at org.glassfish.persistence.common.PersistenceHelper.lookupPMResource(PersistenceHelper.java:63)
at org.glassfish.persistence.jpa.ProviderContainerContractInfoBase.lookupDataSource(ProviderContainerContractInfoBase.java:71)
at org.glassfish.persistence.jpa.PersistenceUnitInfoImpl.<init>(PersistenceUnitInfoImpl.java:108)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:142)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.<init>(PersistenceUnitLoader.java:107)
at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:925)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:434)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
....

有没有人知道发生了什么以及该怎么做?我读到我需要下载 Mysql 的驱动程序并将其放在 lib 文件夹中,但没有任何改变

【问题讨论】:

【参考方案1】:

持久化 xml 文件应该有&lt;jta-data-source&gt;jdbc/JavaProjectRessource&lt;/jta-data-source&gt;

【讨论】:

现在打印Invalid resource : jdbc/JavaProjectRessource__pm 这是因为资源还没有注册。在ApplicationLifecycle.deploy 中,它调用prepareModule(参见问题),然后才调用绑定名称的ApplicationInfo.load

以上是关于Mysql/Glassfish 资源 jdbc 无效的主要内容,如果未能解决你的问题,请参考以下文章

利用ShardingSphere-JDBC实现分库分表

JDBC Oracle Thin ORA-02396 连接空闲超时

从无状态 Bean 中获取 JDBC 连接对象

JDBC连接数据库中文查询无结果

使用JDBC中的出现的乱码和查询无结果问题

jdbc读取资源文件