flyway迁移hsqldb到mysql,使用HSQL在flyway中进行Changin DB事务控制

在Flyway中更改HSQLDB的事务控制时遇到问题,由于Flyway在迁移前后会设置autocommit为false并执行其自身语句,导致迁移中的事务控制语句无法正常执行,使应用程序挂起。解决方案是使用Flyway的回调机制,在`beforeMigrate`和`afterMigrate`回调中分别设置事务控制为LOCKS和MVCC,从而成功在迁移过程中切换事务模型。
摘要由CSDN通过智能技术生成

In HSQL to change TRANSACTION CONTROL there can't be any active transactions.

Flyway, in turn, after committing migration X and before executing SQL from migration X, sets autocommitt=false and executes some of its own statements. So if the migration contains SET DATABASE TRANSACTION CONTROL statement it will wait for those uncommitted statements forever causing application to hang.

(Side note: The statements executed by flyway before migration varies from version to version e.g. in 1.7 that were pure selects so changing from LOCK to MVCC was possible but after I had MVCC any subsequent DDL statements in further migrations hanged; in flyway 2.0 it was select for update on schema_version table so any transaction control change hanged; in 2.2 select for update was changed to explicit lock with the same effect as in 2.0)

So basically it is not possible to change transaction control in flyway migrations. On the other hand flyway discourages changes outside of its migration. Any idea then how to change transaction control in with flyway/hsql?

Update

Another observation is that when database control is set to MVCC then any DDL statement in flyway migration hangs application too. So I would just set LOCKS before each migration and restore MVCC after it. Would that be clean solution from Flyway perspective?

import com.googlecode.flyway.core.util.jdbc.JdbcUtils;

public void migrate() {

setDbTransactionControl("LOCKS");

flyway.migrate();

setDbTransactionControl("MVCC");

}

private void setDbTransactionControl(String mode) {

Connection connection = null;

try {

connection = JdbcUtils.openConnection(ds);

connection.createStatement().execute("SET DATABASE TRANSACTION CONTROL " + mode);

} catch (SQLException e) {

//log it

JdbcUtils.closeConnection(connection);

} finally {

JdbcUtils.closeConnection(connection);

}

}

解决方案

Try to use the Flyway callbacks beforeMigrate and afterMigrate. Both run apart from the migration transactions. MVCC should be used for my application so the the JDBC URL contains hsqldb.tx=mvcc. I could sucessfully change the transaction model during the Flyway migration with beforeMigrate.sql SET DATABASE TRANSACTION CONTROL LOCKS; and afterMigrate.sql SET DATABASE TRANSACTION CONTROL MVCC;. There are also Java versions of the callbacks. I'm using HSQLDB 2.3.3 and Flyway 3.2.1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值