2010-07-13 56 views
3

我想写一个等效的Rails数据模型演化/回滚机制使用Spring Jdbc。春季Jdbc原子性与改变表

了Spring JDBC transactionnal insert/replace作品非常好(的DataSourceTransactionManager与PROPAGATION_REQUIRED下MySQL的InnoDB的5):

// Transaction begins 
getJdbcTemplate().execute("replace into aTable ..."); 
getJdbcTemplate().execute("wrong request"); 
getJdbcTemplate().execute("replace into aTable ..."); 
// none are commited 

alter不会:

// Transaction begins 
getJdbcTemplate().execute("alter table aTable add column `columnForTest` ..."); 
getJdbcTemplate().execute("wrong request"); 
getJdbcTemplate().execute("alter table aTable add column `columnForTest` ..."); 
// the first alter is commited 

有没有办法实现原子性(全 - 或 - 无行为)与alter?预先

+0

在运行时添加数据库列是一个巨大的数据模型设计气味。你不应该使用链接表吗?这样您只需将“列名称”和所需的关联数据作为新行插入即可。 – BalusC 2010-07-14 11:45:54

回答

4

ALTER TABLE(和其它DDL操作)

由于通常非事务性,取决于数据库。 Spring和JDBC无法控制这个。如果在交易中执行非交易操作,则它将以非交易方式执行。

所以它涉及到数据库,以及它如何配置,而不是客户端的问题。

+1

谢谢,我会朝这个方向看。 – 2010-07-14 18:08:02