Glassfish 4 JDBC 资源连接失败,Hibernate 4.3.5 和 JPA 2.1 连接分配由于数据库“null”而被拒绝

Posted

技术标签:

【中文标题】Glassfish 4 JDBC 资源连接失败,Hibernate 4.3.5 和 JPA 2.1 连接分配由于数据库“null”而被拒绝【英文标题】:Glassfish 4 JDBC Resource connection fails with Hibernate 4.3.5 and JPA 2.1 connection allocation refused due to database "null" 【发布时间】:2014-05-25 17:48:35 【问题描述】:

我是 Java EE 的新手,我已经使用 NetBeans 8.0 建立了一个项目。在我的 EAR 项目中,我创建了一个 JDBC DataSource 和 ConnectionPool,它们在部署时成功部署到 Glassfish 服务器。

当我尝试连接时,我使用的是带有 Hibernate 4.3.5 的 JPA 2.1,并且我使用 persistence.xml 文件中的 JTA jta-data-source 条目来命名托管资源。

我的persistence.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="my_PU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/myDatasource</jta-data-source>
        <class>my.domain.model.MyModel</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.connection.datasource" value="jdbc/myDatasource" />
            <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
            <property name="hibernate.connection.autocommit" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

当我尝试部署项目时,一切顺利,直到 Hibernate hbm2ddl 启动,并出现以下错误消息:

INFO:   visiting unvisited references
[...]
INFO:   visiting unvisited references
INFO:   HHH000204: Processing PersistenceUnitInfo [
            name: my_PU
            ...]
INFO:   HHH000412: Hibernate Core 4.3.5.Final
INFO:   HHH000206: hibernate.properties not found
INFO:   HHH000021: Bytecode provider name : javassist
WARN:   HHH000193: Overriding hibernate.transaction.factory_class is dangerous, this might break the EJB3 specification implementation
INFO:   HCANN000001: Hibernate Commons Annotations 4.0.4.Final
WARNING:   RAR5038:Unexpected exception while creating resource for pool myConnectionPool. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: FATAL: database "null" does not exist
WARNING:   RAR5117 : Failed to obtain/create connection from connection pool [ myConnectionPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: FATAL: database "null" does not exist
WARNING:   RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: FATAL: database "null" does not exist]
WARN:   HHH000342: Could not obtain connection to query metadata : Error in allocating a connection. Cause: Connection could not be allocated because: FATAL: database "null" does not exist
INFO:   HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
INFO:   HHH000422: Disabling contextual LOB creation as connection was null
INFO:   HHH000397: Using ASTQueryTranslatorFactory
INFO:   HHH000228: Running hbm2ddl schema update
INFO:   HHH000102: Fetching database metadata
WARNING:   RAR5038:Unexpected exception while creating resource for pool myConnectionPool. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: FATAL: database "null" does not exist
WARNING:   RAR5117 : Failed to obtain/create connection from connection pool [ myConnectionPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: FATAL: database "null" does not exist
WARNING:   RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: FATAL: database "null" does not exist]
ERROR:   HHH000319: Could not get database metadata
java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: FATAL: database "null" does not exist
    at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:121)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:522)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
    at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:199)
    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:922)
    at org.glassfish.javaee.full.deployment.EarDeployer.prepareBundle(EarDeployer.java:307)
    at org.glassfish.javaee.full.deployment.EarDeployer.access$200(EarDeployer.java:88)
    at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:153)
    at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:150)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnBundles(EarDeployer.java:230)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllTypedBundles(EarDeployer.java:239)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllBundles(EarDeployer.java:265)
    at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:150)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:922)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:431)
    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:527)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:356)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.Abstractiostrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)
Caused by: javax.resource.spi.ResourceAllocationException: Error in allocating a connection. Cause: Connection could not be allocated because: FATAL: database "null" does not exist
    at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:319)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:196)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:171)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:166)
    at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:114)
    ... 61 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: FATAL: database "null" does not exist
    at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:103)
    at com.sun.enterprise.resource.pool.ConnectionPool.addResource(ConnectionPool.java:282)
    at com.sun.enterprise.resource.pool.ConnectionPool.createResourceAndAddToPool(ConnectionPool.java:1512)
    at com.sun.enterprise.resource.pool.ConnectionPool.createResources(ConnectionPool.java:944)
    at com.sun.enterprise.resource.pool.ConnectionPool.initPool(ConnectionPool.java:230)
    at com.sun.enterprise.resource.pool.ConnectionPool.internalGetResource(ConnectionPool.java:511)
    at com.sun.enterprise.resource.pool.ConnectionPool.getResource(ConnectionPool.java:381)
    at com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:245)
    at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:360)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307)
    ... 65 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: FATAL: database "null" does not exist
    at com.sun.enterprise.resource.pool.ConnectionPool.createSingleResource(ConnectionPool.java:924)
    at com.sun.enterprise.resource.pool.ConnectionPool.createResource(ConnectionPool.java:1189)
    at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:98)
    ... 75 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: FATAL: database "null" does not exist
    at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:110)
    at com.sun.enterprise.resource.pool.ConnectionPool.createSingleResource(ConnectionPool.java:907)
    ... 77 more
Caused by: javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: FATAL: database "null" does not exist
    at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:129)
    at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:87)
    ... 78 more
Caused by: org.postgresql.util.PSQLException: FATAL: database "null" does not exist
    at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:572)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:177)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:393)
    at org.postgresql.Driver.connect(Driver.java:267)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:95)
    at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:78)
    at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:115)
    ... 79 more

...

【问题讨论】:

我假设 hibernate.properties not found 不相关,因为它看起来像是从 persistence.xml 文件中获取的属性 【参考方案1】:

看起来您在 Glassfish 中的连接池配置存在根本问题。

检查属性等,并确保您可以从管理控制台 PING 数据库。

【讨论】:

完美,翻了一番,发现是NetBeans为部署的JDBC连接池创建细节的方式。我父项目中sun-resources.xmljdbc-connection-pool 元素包含3 个属性;服务器 URL、用户名和密码。这些并没有全部使用,连接似乎在寻找单独的属性,即ServerNamePortNumberDatabaseNameUserPassword,并创建 URL 本身。非常感谢威尔!【参考方案2】:

本指南用于在 NetBeans.8.0 IDE 中集成 hibernate.4.3.5 和 EJB 和 GlassFish.4.0。 在net beans中创建一个web项目,并在项目中添加hibernate jar文件,其他配置mysql和glassfish的设置非常简单,本文不再赘述,然后创建persistence.xml文件如下:

<persistence-unit name="omidashouriPU" transaction-type="Resource_Local">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/YourSchemaName"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="yourpassword"/>
        <property name="hibernate.show_sql" value="true"/>
    </properties>
</persistence-unit>

在用于创建 EntityManager 的 EJB 类(使用 @Stateless 注释的类)中,使用以下语法:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("omidashouriPU");
EntityManager em = emf.createEntityManager();
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(YourEntityObject);
em.getTransaction().end();

如您所知,当您使用“transaction-type="Resource_Local"时,您必须自己管理事务,也就是说,管理打开和关闭事务是我们的责任。

【讨论】:

以上是关于Glassfish 4 JDBC 资源连接失败,Hibernate 4.3.5 和 JPA 2.1 连接分配由于数据库“null”而被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

Glassfish RAR5035:从池中销毁资源时出现意外异常

Glassfish中的JDBC连接池ping错误

无法使用Glassfish3上的JDBC连接到数据库

GlassFish 3.1.1 - 获取 jdbc 连接

Glassfish“连接已关闭”错误与连接池、JDBC 和 SQL Server 2008

glassfish JDBC 连接池