2010-07-13 53 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