简体   繁体   English

在Java中,应用更改集后,Liquibase更新被挂起

[英]From within Java, Liquibase update is hanging after applying a changeset

I am running migrations for unit tests with Liquibase. 我正在使用Liquibase运行迁移以进行单元测试。 I use a class called ${projectName}Liquibase.java to store two static functions 我使用名为$ {projectName} Liquibase.java的类来存储两个静态函数

public class ${projectName}Liquibase {
  ...
  public static void runMigrations(Connection conn, DB_TYPE dbType) {
           Liquibase liquibase;
    Database database = null;
    try {
        database = DatabaseFactory.getInstance()
                                  .findCorrectDatabaseImplementation(new JdbcConnection(conn));
        liquibase = new Liquibase(dbType.filePath, new FileSystemResourceAccessor(), database);
        liquibase.validate();
        liquibase.update(null);
    } catch (LiquibaseException e) {
        throw new RuntimeException("File at " + dbType.filePath + " Error: " + e.getMessage());
    }
  }
  public static void dropTables() {
    ...
  }
}

I pick up the file dbType.filePath parameter by using System.getProperty("user.dir") and the rest of the path. 我通过使用System.getProperty(“ user.dir”)和路径的其余部分来获取文件dbType.filePath参数。

The file is read fine, however, the update only goes through the very first changeset and then hangs for the duration of the test. 可以很好地读取文件,但是,更新仅通过第一个变更集,然后在测试期间挂起。 Thus, the test does not run. 因此,该测试无法运行。

Tests are run successfully from other files and submodules within my Intellij project. 从Intellij项目中的其他文件和子模块成功运行了测试。 In particular, our integration test suite is run successfully using the same interface from a different submodule. 特别是,我们的集成测试套件可使用来自不同子模块的相同接口成功运行。 All of the tests will pass up until this one: 所有测试将一直持续到此:

Running *.*.*.*.*.*DAOTest
2013-11-03 14:59:53,144 DEBUG [main] c.j.bonecp.BoneCPDataSource   : JDBC URL = jdbc:hsqldb:mem:*, Username = SA, partitions = 2, max (per partition) = 5, min (per partition) = 5, helper threads = 3, idle max age = 60 min, idle test period = 240 min
INFO 11/3/13 2:59 PM:liquibase: Reading from PUBLIC.DATABASECHANGELOG
INFO 11/3/13 2:59 PM:liquibase: Successfully acquired change log lock
INFO 11/3/13 2:59 PM:liquibase: Reading from PUBLIC.DATABASECHANGELOG
INFO 11/3/13 2:59 PM:liquibase: /Users/davidgroff/repo/services/${projectName}/server/../core/src/main/java/com/*/*/liquibase/hsqldb.sql: 1::davidgroff: Custom SQL executed
INFO 11/3/13 2:59 PM:liquibase: /Users/davidgroff/repo/services/${projectName}/server/../core/src/main/java/com/*/*/liquibase/hsqldb.sql: 1::davidgroff: ChangeSet /Users/davidgroff/repo/services/*/*/../core/src/main/java/com/*/*/liquibase/hsqldb.sql::1::davidgroff ran successfully in 3ms
INFO 11/3/13 2:59 PM:liquibase: Successfully released change log lock

After this, the test repeatedly hangs as in in some infinite loop. 此后,测试会像在某个无限循环中一样反复挂起。

I have the current setup: 我有当前设置:

      <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>3.0.6</version>
      </dependency>
      <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>3.0.6</version>
      </dependency>

I'm using Java 7 on Maven 3.1.0. 我在Maven 3.1.0上使用Java 7。

It may be that a separate transaction has locked a row in your database and Liquibase is hanging waiting for the other transaction to complete. 可能是一个单独的事务已锁定数据库中的一行,而Liquibase挂起以等待其他事务完成。

You said "the update only goes through the very first changeset and then hangs for the duration of the test", does that mean the first changeSet runs successfully? 您说“更新仅通过第一个变更集,然后在测试期间挂起”,这是否意味着第一个changeSet成功运行? If that is the case, then the locked record is either a table lock on the DATABASECHANGELOG table that is preventing the INSERT INTO DATABASECHANGELOG from completing or a problem with your second changeSet. 如果是这种情况,则锁定的记录要么是DATABASECHANGELOG表上的表锁,它阻止了INSERT INTO DATABASECHANGELOG的完成,要么是您的第二个changeSet有问题。

Assuming it is a problem with the DATABASECHANGELOG table, is there a separate thread or process that would have been trying to delete from that table? 假设DATABASECHANGELOG表存在问题,是否有单独的线程或进程一直试图从该表中删除?

The issue turned out to be that a connection was being created and used after the liquibase changeset was applied with the command, 原来的问题是,在使用以下命令应用了liquibase变更集之后,正在创建并使用连接,

connection.createStatement(..."***SQL***"...);

and was never being committed to the database, because a new connection was created or that connection was out of data. 并且从未被提交到数据库,因为创建了新连接或该连接已用完数据。 It is a mystery why this was working before we used Liquibase to run migrations. 为什么这在我们使用Liquibase运行迁移之前起作用的原因还是一个谜。 The fix is simply to commit the above statement by calling: 解决方法是通过调用以下命令来提交上述语句:

connection.commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM