简体   繁体   中英

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

I am running migrations for unit tests with Liquibase. I use a class called ${projectName}Liquibase.java to store two static functions

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.

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

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?

The issue turned out to be that a connection was being created and used after the liquibase changeset was applied with the command,

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. The fix is simply to commit the above statement by calling:

connection.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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