我是Java EE的新手,我使用NetBeans 8.0设置了一个项目。在我的EAR项目中,我创建了一个JDBC DataSource和ConnectionPool,它们在部署时成功部署到Glassfish服务器。
当我尝试连接时,我正在使用JPA 2.1和Hibernate 4.3.5,并且我在jta-data-source
文件中使用JTA persistence.xml
条目来命名托管资源。
我的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
...
答案 0 :(得分:1)
看起来Glassfish中的连接池配置存在根本错误。
去检查属性等,并确保您可以从管理控制台PING数据库。
答案 1 :(得分:0)
本指南适用于在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>
在您的EJB类(使用@Stateless注释的类)中创建EntityManager时使用以下语法:
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“时,您必须自己管理交易,这意味着,管理开启和关闭交易是我们的责任。