在Mybatis迁移工具中的一个事务中运行多个mysql语句

时间:2011-03-21 15:55:18

标签: java mysql ibatis mybatis

我正在使用Mybatis迁移工具来维护我们数据库的架构,但是我遇到了以下问题。

目前,如果我们在迁移中使用多个语句,则它们分别在单独的事务中运行。因此,如果我想要更改2个表(或运行多个语句)作为功能的一部分并且其中一个中断,则必须手动还原任何首先运行的表。但是,如果所有语句都成功完成,则mybatis迁移仅在changelog表中标记为完成。

这实在令人沮丧,因为如果整个迁移不是自治的,就无法维持一个常量的db状态。

设置

这是我们的测试数据库的mybatis mygration的(相关)设置。

## JDBC connection properties.
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/gamealert_test?allowMultiQueries=true
username=gamealert
password=********

# If set to true, each statement is isolated
# in its own transaction.  Otherwise the entire
# script is executed in one transaction.
auto_commit=false

# This controls how statements are delimited.
# By default statements are delimited by an
# end of line semicolon.  Some databases may
# (e.g. MS SQL Server) may require a full line
# delimiter such as GO.
delimiter=;
full_line_delimiter=false

# This ignores the line delimiters and
# simply sends the entire script at once.
# Use with JDBC drivers that can accept large
# blocks of delimited text at once.
send_full_script=true

我已经添加了auto_commit = false,send_full_script = true和allowMultiQueries = true(到url),试图将整个迁移保留在一个事务中。

我是否需要使用mysql url参数来实现此目的?这甚至可能吗?看起来应该是这样。也许我们只需要为每个语句创建一个迁移,但这似乎过多了。

实施例

这是澄清的另一个例子

示例迁移20110318154857_fix_daily_sales:

--// fix daily_sales naming
-- Migration SQL that makes the change goes here.

ALTER TABLE `daily_sales` CHANGE COLUMN `storeId` `store_id` INT(10) UNSIGNED NOT NULL;

b0rked;

--//@UNDO
-- SQL to undo the change goes here.
... undo sql here ....

如果我运行迁移,则由于b0rked;行符合预期而失败。 迁移状态显示迁移为预期的挂起。

20110318130407 2011-03-18 17:06:24 create changelog
20110318144341 2011-03-18 17:06:30 fix schedule naming
20110318154857    ...pending...    fix daily sales naming

我的数据库却应用了更改! 不好!

describe daily_sales;
+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| store_id  | int(10) unsigned | NO   | PRI | NULL    |       |
| sale_date | date             | NO   | PRI | NULL    |       |
| type_id   | int(10) unsigned | NO   | PRI | NULL    |       |
| tokens    | int(10) unsigned | NO   |     | 0       |       |
| dollars   | double           | NO   |     | 0       |       |
+-----------+------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

有什么方法可以阻止这种情况吗?我应该将每个语句放在迁移中并继续吗?那就是我现在的位置。

提前致谢。

1 个答案:

答案 0 :(得分:2)

DML绝不是交易性的 - 立即应用。无法回滚