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

标签 java mysql ibatis mybatis

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

目前,如果我们在迁移中使用多个语句,它们各自在一个单独的事务中运行。因此,如果我想更改 2 个表(或运行多个语句)作为功能的一部分并且其中一个中断,则必须手动恢复首先运行的任何表。然而,如果所有语句都成功完成,则 mybatis 迁移只会在变更日志表中标记为完成。

这真的很令人沮丧,因为如果整个迁移不是自主的,就没有办法维持一个恒定的数据库状态。

设置

这是我们测试数据库的 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 ....

如果我运行 migrate up 它会失败,因为 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)

有什么办法可以避免这种情况吗?我应该把每条语句都放在迁移中然后继续吗?那就是我现在的位置。

提前致谢。

最佳答案

DML 永远不是事务性的——立即应用。没有办法回滚

关于java - Mybatis迁移工具在一个事务中运行多条mysql语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380201/

相关文章:

java - Apache Commons SCXML - 找不到 JexlContext

java - RFC5545。同时计算 RRULE 和 EXDATE (EXRULE) 的事件发生次数

java - 结合 hasNextInt 和值大于/小于 X 检查

mysql - MgmtSrvr错误—无法确定要用于此节点的节点ID。在命令行上使用--ndb-nodeid = <nodeid>进行指定

java - Ibatis绑定(bind)异常错误信息

java - 是否可以在第三方类上切入点以及如何切入?

java - 使用Java获取Kubernetes API key

PHP - 使用 PDO 构建类 - 警告 : Missing argument

java - 从java如何调用存储过程传递oracle游标作为参数

mysql - 如何在这条sql语句上创建索引?