0

我正在使用 Liquibase 进行单元测试的迁移。我使用一个名为 ${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() {
    ...
  }
}

我通过使用 System.getProperty("user.dir") 和路径的其余部分来获取文件 dbType.filePath 参数。

该文件可以正常读取,但是,更新仅通过第一个变更集,然后在测试期间挂起。因此,测试不会运行。

测试从我的 Intellij 项目中的其他文件和子模块成功运行。特别是,我们的集成测试套件使用来自不同子模块的相同接口成功运行。所有的测试都将通过,直到这个:

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

在此之后,测试反复挂起,就像在某个无限循环中一样。

我有当前的设置:

      <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>

我在 Maven 3.1.0 上使用 Java 7。

4

2 回答 2

2

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.

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? 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.

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?

于 2013-11-04T14:33:07.790 回答
0

问题原来是在使用命令应用 liquibase 变更集后正在创建和使用连接,

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

并且从未提交到数据库,因为创建了新连接或该连接没有数据。在我们使用 Liquibase 运行迁移之前,为什么它会起作用是一个谜。修复只是通过调用提交上述语句:

connection.commit();
于 2013-11-05T22:51:47.897 回答