MyBatis移行ツールで1つのトランザクションで複数のMySQLステートメントを実行する
質問
MyBatis移行ツールを使用してスキーマをデータベースに維持していますが、次の問題があります。
現在、移行で複数のステートメントを使用すると、それぞれ別のトランザクションで実行されます。したがって、機能の一部として2つのテーブル(または複数のステートメントを実行する)を変更し、そのうちの1つが最初に実行されたものを手動で戻す必要があります。ただし、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、apposMultiqueries = true(url)を1つのトランザクションで維持するために追加しました。
これを許可するために使用する必要があるMySQL URLパラメーターはありますか?これも可能ですか?そうあるべきだと思われます。たぶん、各ステートメントに対して1つの移行を作成する必要があるかもしれませんが、それは過度に思えます。
例
説明のさらなる例を次に示します
例Migration 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)
これを防ぐ方法はありますか?各ステートメントを移行に配置して先に進む必要がありますか?それは私が今いるところです。
前もって感謝します。
解決
DMLは決してトランザクションではありません - すぐに適用されます。ロールバックする方法はありません