Payara Micro 忽略 persistence.xml 中的连接 url

Payara Micro ignoring connection url in persistence.xml

我正在编写一个 Payara Micro Web 应用程序,它应该连接到 persistence.xml 中定义的数据库。它应该连接到的数据库是我本地机器上的一个文件,在项目启动时,它不存在。据我了解,H2 应该在第一次连接时为数据库创建一个文件。

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="com.danjbower_h2test_war_0.0.1PU"
                      transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>com.danjbower.h2test.Test</class>

        <properties>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:file:C:/databases/testdb" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />

            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
            <property name="hibernate.transaction.jta.platform" value="SunOne" />
        </properties>
    </persistence-unit>
</persistence>

当项目是 运行 时,它连接到数据库并运行 sql 命令来创建表并添加我想要添加的特定实体,定义在启动 bean 中。但是,当我检查我的系统时,它没有像我预期的那样创建 C:\databases\testdb.mv.db 文件。出了什么问题,我该怎么做才能真正生成数据库文件,而不是看似忽略配置?


MCVE 的其他文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.danjbower</groupId>
    <artifactId>H2Test</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>

    <name>H2Test</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <version.payara>5.201</version.payara>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>fish.payara.api</groupId>
                <artifactId>payara-bom</artifactId>
                <version>${version.payara}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-web-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.enterprise.concurrent</groupId>
            <artifactId>jakarta.enterprise.concurrent-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.resource</groupId>
            <artifactId>jakarta.resource-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.batch</groupId>
            <artifactId>jakarta.batch-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.microprofile</groupId>
            <artifactId>microprofile</artifactId>
            <scope>provided</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.25.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.25.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>fish.payara.maven.plugins</groupId>
                <artifactId>payara-micro-maven-plugin</artifactId>
                <version>1.0.6</version>
                <configuration>
                    <payaraVersion>${version.payara}</payaraVersion>
                    <deployWar>false</deployWar>
                    <commandLineOptions>
                        <option>
                            <key>--autoBindHttp</key>
                        </option>
                        <option>
                            <key>--deploy</key>
                            <value>${project.build.directory}/${project.build.finalName}</value>
                        </option>
                    </commandLineOptions>
                    <contextRoot>/H2Test</contextRoot>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>payara-patched-externals</id>
            <name>Payara Patched Externals</name>
            <url>https://raw.github.com/payara/Payara_PatchedProjects/master</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

Test.java

package com.danjbower.h2test;

import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "Tests")
@XmlRootElement
public class Test
{
    @Id
    @Column(name = "Id", unique = true)
    @Basic(optional = false)
    @NotNull
    private Integer id = 0;

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    @Override
    public int hashCode()
    {
        return id;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }

        if (obj == null)
        {
            return false;
        }

        if (getClass() != obj.getClass())
        {
            return false;
        }

        final Test other = (Test) obj;

        return Objects.equals(id, other.id);
    }

    @Override
    public String toString()
    {
        return "Test (Id: " + id + ")";
    }
}

StartUp.java

package com.danjbower.h2test;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Startup
@Singleton
public class StartUp
{
    @PersistenceContext(unitName = "com.danjbower_h2test_war_0.0.1PU")
    private EntityManager entityManager;

    @PostConstruct
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void init()
    {
        Test test = new Test();
        test.setId(4);
        entityManager.persist(test);
    }
}

日志显示 运行 命令

Hibernate: 
    
    create table Tests (
       Id integer not null,
        primary key (Id)
    )

Hibernate: 
    insert 
    into
        Tests
        (Id) 
    values
        (?)


[2021-03-07T03:37:03.511+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1615088223511] [levelValue: 800] 
{
    "Instance Configuration": {
        "Host": "192.168.1.12",
        "Http Port(s)": "8080",
        "Https Port(s)": "",
        "Instance Name": "Hilarious-Boxfish",
        "Instance Group": "MicroShoal",
        "Hazelcast Member UUID": "ae8dbc3a-b361-4dd6-bbba-762e620f96a8",
        "Deployed": [
            {
                "Name": "H2Test-0.0.1",
                "Type": "war",
                "Context Root": "/H2Test"
            }
        ]
    }
}

[2021-03-07T03:37:03.513+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1615088223513] [levelValue: 800] 
Payara Micro URLs:
http://192.168.1.12:8080/H2Test

问题是您正在使用 transaction-type="JTA",这意味着数据源是从 Payara Micro 检索的,而不是由 Hibernate 根据您指定的属性创建的。

您应该将 transaction-type="JTA" 更改为 transaction-type="RESOURCE_LOCAL"。这应该会修复它并按您预期的方式运行。

现在发生的事情是 Hibernate 从 Payara Micro 请求数据源并忽略 hibernate.connection 属性。您没有指定任何数据源 JNDI 名称(例如使用 hibernate.connection.datasource),因此它将检索默认数据源。 Payara Micro 中的默认数据源用于存储在临时目录中的 H2 数据库。

可以在此处找到项目的工作示例 github.com/DanJBower/H2Test


此问题的一个解决方案是设置 jta-data-source 而不是使用 hibernate.connection.* 属性。

这意味着将 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="com.danjbower_h2test_war_0.0.1PU"
                      transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <jta-data-source>java:app/TestDb</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.transaction.jta.platform" value="SunOne" />
        </properties>
    </persistence-unit>
</persistence>

并添加一个名为 payara-resources.xml 的文件。这是建立数据库连接的地方。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Community-Documentation/master/docs/modules/ROOT/pages/schemas/payara-resources_1_6.dtd">
<resources>
    <jdbc-resource pool-name="TestDb"
                   jndi-name="java:app/TestDb"
                   enabled="true" />

    <jdbc-connection-pool datasource-classname="org.h2.jdbcx.JdbcDataSource"
                          name="TestDb"
                          res-type="javax.sql.DataSource">
        <property name="URL" value="jdbc:h2:file:C:/databases/TestDb" />
        <property name="User" value="sa" />
        <property name="Password" value="" />
    </jdbc-connection-pool>
</resources>

备注

最初我把它放在 web.xml 作为

<data-source>
    <name>java:app/TestDb</name>
    <class-name>org.h2.Driver</class-name>
    <url>jdbc:h2:file:C:/databases/TestDb</url>
    <user>sa</user>
    <password></password>
</data-source>

但是,在执行此操作时,我遇到了一个错误,提示未设置密码。与此非常相似 issue.