Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA持久性配置错误_Jpa_Ejb 3.0_Persistence.xml - Fatal编程技术网

JPA持久性配置错误

JPA持久性配置错误,jpa,ejb-3.0,persistence.xml,Jpa,Ejb 3.0,Persistence.xml,我正在eclipse中使用Glashfish服务器运行一个独立的EJB项目。但它抛出了一个与我配置的数据库无关的错误。我使用PostgreSQL和hibernate作为JPA提供者。注意,我只是在eclipse中部署和运行应用程序 这是我的persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence"

我正在eclipse中使用Glashfish服务器运行一个独立的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>

摘自JPA规范(第8.2.1.2节):

交易类型 事务类型属性用于指定实体管理器工厂为持久化单元提供的实体管理器必须是JTA实体管理器还是资源本地实体管理器。此元素的值为JTA或RESOURCE_LOCAL。JTA的事务类型假定将提供JTA数据源—由JTA数据源元素指定或由容器提供。通常,在JavaEE环境中,事务类型的RESOURCE_LOCAL假设将提供非JTA数据源。在JavaEE环境中,如果未指定此元素,则默认为JTA。在Java SE环境中,如果未指定此元素,则默认为RESOURCE_LOCAL

您尚未指定事务类型,并且正在JavaEE环境中运行。因此,缺省值是JTA(这是正确使用的东西)

因为它是JTA,所以使用了JTA数据源。但是您还没有配置一个,所以使用了容器中配置的默认数据源

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

    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.