小编典典

将HSQLDB与SQL Maven插件和jOOQ一起使用

sql

这与类似的问题类似,例如在Maven中使用嵌入式数据库与Flyway和jOOQ进行持续集成,尽管与我们使用而sql-maven-plugin不是Flyway并不完全相同。

以下Maven插件配置失败:

sql-maven-plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.5</version>

    <executions>
        <execution>
            <id>create-database</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <driver>${db.driver}</driver>
                <url>${db.url}</url>
                <username>${db.username}</username>
                <password>${db.password}</password>
                <srcFiles>
                    <srcFile>src/main/resources/sql/create-db.sql</srcFile>
                </srcFiles>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>${hsqldb.version}</version>
        </dependency>
    </dependencies>
</plugin>

jooq-codegen-maven

<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <version>${jooq.version}</version>

    <executions>
        <execution>
            <id>generate-hsqldb</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <jdbc>
                    <driver>${db.driver}</driver>
                    <url>${db.url}</url>
                    <user>${db.username}</user>
                    <password>${db.password}</password>
                </jdbc>
                <generator>
                    <database>
                        <inputSchema>PUBLIC</inputSchema>
                    </database>
                    <target>
                        <packageName>com.example.db</packageName>
                        <directory>target/generated-sources/jooq-hsqldb</directory>
                    </target>
                </generator>
            </configuration>
        </execution>
    </executions>
</plugin>

错误是:

java.sql.SQLException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile@cc76d81d[file =C:\Users\Lukas\example.db.lck, exists=true, locked=false, valid=false, ] method: checkHeartbeat read: 2015-10-14 10:49:15 heartbeat - read: -9975 ms.
        at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
        at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
        at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
        at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
        at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
        at org.jooq.util.GenerationTool.run(GenerationTool.java:208)
        at org.jooq.util.GenerationTool.generate(GenerationTool.java:180)
        at org.jooq.util.maven.Plugin.execute(Plugin.java:131)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

一个人如何解决这个问题?


阅读 191

收藏
2021-05-16

共1个答案

小编典典

就像在另一个问题中解释的那样,在Flyway中使用jOOQ时,HSQLDB在基于类加载器的基础上锁定数据库存在问题。这里涉及的两个Maven插件不共享相同的类加载器,因此,第二个插件不能访问由第一个类加载器嵌入的数据库,除非:

  • SHUTDOWN IMMEDIATELY 在第一个脚本中被调用(但这有点丑陋,因为在其他情况下也可能需要该脚本)
  • JVM已关闭(但是在Maven的上下文中这非常丑陋)

一种解决方法是在中指定第二个执行,该执行sql-maven-plugin显式调用以下SHUTDOWN IMMEDIATELY命令:

<execution>
    <id>create-database</id>
    <phase>generate-sources</phase>
    ...
</execution>

<execution>
    <id>shutdown-database</id>
    <phase>generate-sources</phase>
    <goals>
        <goal>execute</goal>
    </goals>
    <configuration>
        <driver>${db.driver}</driver>
        <url>${db.url}</url>
        <username>${db.username}</username>
        <password>${db.password}</password>

        <sqlCommand>SHUTDOWN IMMEDIATELY</sqlCommand>
    </configuration>
</execution>
2021-05-16