Java 在Mybatis迁移工具的一个事务中运行多个mysql语句
我正在使用Mybatis迁移工具来维护数据库的模式,但我有以下问题 目前,如果我们在迁移中使用多个语句,它们都在单独的事务中运行。因此,如果我想将两个表(或运行多个语句)作为一个功能的一部分进行更改,而其中一个表中断,则必须手动恢复首先运行的任何表。但是,只有在所有语句都成功完成的情况下,mybatis迁移才会在changelog表中标记为完成 这确实令人沮丧,因为如果整个迁移不是自治的,就无法保持恒定的db状态 设置 下面是我们测试数据库的mybatis mygration的(相关)设置Java 在Mybatis迁移工具的一个事务中运行多个mysql语句,java,mysql,ibatis,mybatis,Java,Mysql,Ibatis,Mybatis,我正在使用Mybatis迁移工具来维护数据库的模式,但我有以下问题 目前,如果我们在迁移中使用多个语句,它们都在单独的事务中运行。因此,如果我想将两个表(或运行多个语句)作为一个功能的一部分进行更改,而其中一个表中断,则必须手动恢复首先运行的任何表。但是,只有在所有语句都成功完成的情况下,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和allowmultiquerys=true(到url),试图将整个迁移保持在一个事务中
我需要使用任何mysql url参数来实现这一点吗?这可能吗?看起来应该是这样。也许我们只需要为每个语句创建一个迁移,但这似乎太过分了
例子
下面是进一步澄清的示例
迁移示例20110318154857_固定_每日_销售:
--// 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从来都不是事务性的——立即应用。无法将其回滚谢谢。我就是这么想的。因此,在这一点上,我的问题的解决方案是每个迁移脚本不超过一条语句。这不是完全正确的。一些数据库供应商允许DDL操作作为原子操作提交或回滚。是的,谢谢您的评论。我的例子是针对MySQL的,所以答案对于MySQL是正确的,但是您也正确。我在另一个项目上使用PostgreSQL,大多数DDL操作都是事务性的。