FlyWay连接关闭后不允许进行任何操作

时间:2019-07-11 15:06:45

标签: mysql prepared-statement flyway

我尝试使用FlyWay 6.0.Beta2 BaseJavaMigration和Mysql

@Slf4j
public class V1_7__ini_cf_template_2019_07_1 extends BaseJavaMigration {

  @Override
  public void migrate(Context context) throws Exception {
    log.info("111");
    try (Connection connection = context.getConnection()) {
      log.info("222");
      try (PreparedStatement statement = connection.prepareStatement(
          "insert into console.customers_settings (settings_customer_id, start_balance, max_balance_allowed, max_instance_allowed) values (?, ?, ?, ?)")) {
        log.info("333");
        statement.setLong(1, 3);
        statement.setBigDecimal(2, new BigDecimal(100));
        statement.setBigDecimal(3, new BigDecimal(200));
        statement.setBigDecimal(4, new BigDecimal(300));
        statement.executeUpdate();
      }
    }
  }
}

由于某种原因,我得到一个错误

无法回滚事务 java.sql.SQLNonTransientConnectionException:连接关闭后不允许进行任何操作。

和某种类型的请求。做得好

https://flywaydb.org/getstarted/java

public class V3__Anonymize extends BaseJavaMigration {
    public void migrate(Context context) throws Exception {
        try (Statement select = context.getConnection().createStatement()) {
            try (ResultSet rows = select.executeQuery("SELECT id FROM person ORDER BY id")) {
                while (rows.next()) {
                    int id = rows.getInt(1);
                    String anonymizedName = "Anonymous" + id;
                    log.info(anonymizedName);
                    }
                }
            }
        }
    }
}

可能是什么原因

1 个答案:

答案 0 :(得分:1)

请勿将Connection connection = context.getConnection()放在try内-完成此操作后,连接将在Flyway引擎完成连接之前关闭。通过一些额外的日志记录,您可以看到在迁移完成后 发生了异常。

如果您在此处查看示例:https://flywaydb.org/documentation/migrations#java-based-migrations,您将看到该语句已关闭,但未关闭连接。