SQL 批量更新 - 回滚“CREATE TABLE”?
我有一个升级数据库的方法:
private void executeUpdateBatch(String... sql) throws SQLException {
JdbcConnection connJbdc = new JdbcConnectionImpl();
Connection conn = connJbdc.getConnection();
conn.setAutoCommit(false);
Statement st = conn.createStatement();
for(String s : sql) {
st.addBatch(s);
}
try {
// execute the batch
int[] updateCounts = st.executeBatch();
} catch (BatchUpdateException e) {
int[] updateCounts = e.getUpdateCounts();
checkUpdateCounts(updateCounts);
try {
conn.rollback();
} catch (Exception e2) {
}
throw new SQLException(e);
}
// since there were no errors, commit
conn.commit();
st.close();
conn.close();
}
升级方法:
public void upgradeTo5() throws SQLException {
executeUpdateBatch("CREATE TABLE project ("
+ "id INT(10) unsigned NOT NULL auto_increment, "
+ "title VARCHAR(255) NOT NULL, "
+ "date_from DATE NULL, date_to DATE NULL,"
+ "active BIT NOT NULL, PRIMARY KEY (id))",
"INSERT INTO project(titlea) VALUES('test1')");
}
INSERT
中出现错误,只是为了测试回滚。
好吧,现在的问题是它不会回滚CREATE TABLE项目
。表是InnoDB。有什么建议吗?
I have a method for upgrading DB:
private void executeUpdateBatch(String... sql) throws SQLException {
JdbcConnection connJbdc = new JdbcConnectionImpl();
Connection conn = connJbdc.getConnection();
conn.setAutoCommit(false);
Statement st = conn.createStatement();
for(String s : sql) {
st.addBatch(s);
}
try {
// execute the batch
int[] updateCounts = st.executeBatch();
} catch (BatchUpdateException e) {
int[] updateCounts = e.getUpdateCounts();
checkUpdateCounts(updateCounts);
try {
conn.rollback();
} catch (Exception e2) {
}
throw new SQLException(e);
}
// since there were no errors, commit
conn.commit();
st.close();
conn.close();
}
And upgrade method:
public void upgradeTo5() throws SQLException {
executeUpdateBatch("CREATE TABLE project ("
+ "id INT(10) unsigned NOT NULL auto_increment, "
+ "title VARCHAR(255) NOT NULL, "
+ "date_from DATE NULL, date_to DATE NULL,"
+ "active BIT NOT NULL, PRIMARY KEY (id))",
"INSERT INTO project(titlea) VALUES('test1')");
}
An error is in INSERT
just for testing rollback.
Well, the problem now is that it does not rollbacks CREATE TABLE project
. Table is InnoDB. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySQL/InnoDB 不支持此功能。所有 DDL 语句(
CREATE TABLE
、ALTER TABLE
、CREATE INDEX
、DROP ...
)始终发生在外部交易控制。这是 IIRC Postgres 可以更好处理的一个弱点,但对于 MySQL,您必须通过在回滚时自行恢复更改来解决这个问题。
This is not supported by MySQL/InnoDB. All DDL statements (
CREATE TABLE
,ALTER TABLE
,CREATE INDEX
,DROP ...
) always happen outside of transaction control.This is a weak point that IIRC Postgres can handle better, but with MySQL you have to work around that by reverting the changes yourself in case of rollbacks.