1 21 22 package org.dbunit.operation; 23 24 import org.dbunit.DatabaseUnitException; 25 import org.dbunit.database.IDatabaseConnection; 26 import org.dbunit.dataset.IDataSet; 27 28 import java.sql.Connection ; 29 import java.sql.SQLException ; 30 31 38 public class TransactionOperation extends DatabaseOperation 39 { 40 private final DatabaseOperation _operation; 41 42 45 public TransactionOperation(DatabaseOperation operation) 46 { 47 _operation = operation; 48 } 49 50 53 public void execute(IDatabaseConnection connection, IDataSet dataSet) 54 throws DatabaseUnitException, SQLException 55 { 56 IDatabaseConnection databaseConnection = connection; 57 Connection jdbcConnection = databaseConnection.getConnection(); 58 59 if (jdbcConnection.getAutoCommit() == false) 60 { 61 throw new ExclusiveTransactionException(); 62 } 63 64 jdbcConnection.setAutoCommit(false); 65 try 66 { 67 _operation.execute(databaseConnection, dataSet); 68 jdbcConnection.commit(); 69 } 70 catch (DatabaseUnitException e) 71 { 72 jdbcConnection.rollback(); 73 throw e; 74 } 75 catch (SQLException e) 76 { 77 jdbcConnection.rollback(); 78 throw e; 79 } 80 catch (RuntimeException e) 81 { 82 jdbcConnection.rollback(); 83 throw e; 84 } 85 finally 86 { 87 jdbcConnection.setAutoCommit(true); 88 } 89 } 90 } 91 92 93 94 95 96 | Popular Tags |