JPA持久性配置错误

时间:2017-06-25 07:06:14

标签: jpa ejb-3.0 persistence.xml

我正在使用Glashfish服务器在eclipse中运行独立的EJB项目。但它抛出了与我配置的数据库无关的错误。我使用PostgreSQL和hibernate作为JPA提供程序。请注意,我只是在eclipse中部署和运行应用程序。

这是我的persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="mypu">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/ejb" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="postgres" />
            <!-- <property name="hibernate.show_sql" value="true"/> -->
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

我认为glassfish正在使用一些默认配置而无法识别我的persintence.xml中的内容

错误:

    2017-06-25T14:50:24.835+0800|Info: HHH000204: Processing PersistenceUnitInfo [
    name: mypu
    ...]
2017-06-25T14:50:25.868+0800|Warning: RAR5038:Unexpected exception while creating resource for pool DerbyPool. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
2017-06-25T14:50:25.869+0800|Warning: RAR5117 : Failed to obtain/create connection from connection pool [ DerbyPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
2017-06-25T14:50:25.869+0800|Warning: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.]
2017-06-25T14:50:25.869+0800|WARN: HHH000342: Could not obtain connection to query metadata : Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
2017-06-25T14:50:25.870+0800|Info: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2017-06-25T14:50:25.871+0800|Info: HHH000422: Disabling contextual LOB creation as connection was null
2017-06-25T14:50:25.907+0800|Info: HHH000397: Using ASTQueryTranslatorFactory
2017-06-25T14:50:25.949+0800|Info: HHH000228: Running hbm2ddl schema update
2017-06-25T14:50:25.951+0800|Info: HHH000102: Fetching database metadata
2017-06-25T14:50:26.953+0800|Warning: RAR5038:Unexpected exception while creating resource for pool DerbyPool. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
2017-06-25T14:50:26.953+0800|Warning: RAR5117 : Failed to obtain/create connection from connection pool [ DerbyPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
2017-06-25T14:50:26.953+0800|Warning: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.]
2017-06-25T14:50:26.954+0800|ERROR: HHH000319: Could not get database metadata
java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.

1 个答案:

答案 0 :(得分:1)

摘自JPA规范(第8.2.1.2节):

  

交易类型

     

transaction-type属性用于指定实体管理器工厂为持久性单元提供的实体管理器是否必须是JTA实体管理器或资源本地实体管理器。此元素的值为JTA或RESOURCE_LOCAL。事务类型的JTA假定将提供JTA数据源 - 由jta-data-source元素指定或由容器提供。通常,在Java EE环境中,事务类型RESOURCE_LOCAL假定将提供非JTA数据源。在Java EE环境中,如果未指定此元素,则默认值为JTA。在Java SE环境中,如果未指定此元素,则缺省值为RESOURCE_LOCAL。

您尚未指定事务类型,并且您已在Java EE环境中运行。所以默认是JTA(这是正确的选择)。

由于它是JTA,因此使用了jta-data-source。但是您还没有配置一个,因此使用容器中配置的默认数据源。

您应该在Glassfish服务器中配置PostgreSQL连接池(数据源),并使用该数据源。