2010-12-03 44 views
0

Ich habe eine Methode für die Aktualisierung DB:SQL Batch Update - Rollback auf "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(); 
} 

und Upgrade-Methode:

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')"); 
} 

Ein Fehler ist in INSERT nur zum Testen Rollback.

Nun, das Problem ist jetzt, dass es Rollbacks CREATE TABLE project nicht tut. Tabelle ist InnoDB. Irgendwelche Vorschläge?

Antwort

2

Dies wird von MySQL/InnoDB nicht unterstützt. Alle DDL-Anweisungen (CREATE TABLE, ALTER TABLE, CREATE INDEX, DROP ...) passieren immer außerhalb der Transaktionssteuerung.

Dies ist ein Schwachpunkt, den IIRC Postgres besser handhaben kann, aber mit MySQL müssen Sie umgehen, indem Sie die Änderungen im Falle von Rollbacks selbst rückgängig machen.