X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FDatabaseUtils.java;h=cd7b3e26dd0bd56cc1d943e37ef8262b478ce8ab;hb=185601ca79008d99e5c2293bc3686fc501720484;hp=1b300433d8bc49ca268dc964d5db902b28e1231f;hpb=a8ce4dd755f1d7659f9be3e54754c4c3b93add2c;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java index 1b300433..cd7b3e26 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java @@ -21,6 +21,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.DataSource; @@ -66,9 +67,47 @@ public class DatabaseUtils { private DataSource dataSource; private ITableFilterSimple tables; + private IDatabaseTester dbtester; + /** + * List of connections that were created for dbtesters. + * This list will be closed in the {@link #stop()} method. + */ + private List connections; + + /** + * Constructs the database utils. + * Before use, {@link #start()} must be called. + * @param aDataSource Datasource. + * @param aTables Tables to work with. + */ public DatabaseUtils(DataSource aDataSource, ITableFilterSimple aTables) { dataSource = aDataSource; tables = aTables; + dbtester = new DataSourceDatabaseTester(dataSource); + connections = new ArrayList(); + } + + /** + * Starts the database utils. + */ + public void start() { + // Empty. No operation currently. + } + + /** + * Stops the database utils, closing any JDBC connections that were created + * by this utility. Note that connections obtained from the datasource directly + * must still be closed by the user. The involved connections are only those that + * are created by this utility. + */ + public void stop() { + for (IDatabaseConnection connection: connections) { + try { + connection.close(); + } catch (SQLException e) { + LOG.log(Level.WARNING, "Could not close connection", e); + } + } } public IDatabaseTester createDbTester() throws Exception { @@ -76,8 +115,9 @@ public class DatabaseUtils { } public IDatabaseTester createDbTester(String[] aTables) throws Exception { - IDatabaseTester dbtester = new DataSourceDatabaseTester(dataSource); - dbtester.setDataSet(dbtester.getConnection().createDataSet(aTables)); + IDatabaseConnection connection = dbtester.getConnection(); + connections.add(connection); + dbtester.setDataSet(connection.createDataSet(aTables)); return dbtester; } @@ -90,15 +130,12 @@ public class DatabaseUtils { final String[] tables = getTableNames(aTables); executeInTransaction(new JdbcUnitOfWork() { public Void execute(Connection aConnection) throws Exception { - for (int i = tables.length-1; i >= 0; i--) { + for (int i = tables.length - 1; i >= 0; i--) { aOperation.execute(tables[i]); } return null; } }); - for (String table : tables) { - - } } public void cleanDatabase(ITableFilterSimple aSelection) throws Exception { @@ -123,6 +160,7 @@ public class DatabaseUtils { public T executeInTransaction(JdbcUnitOfWork aCallback) throws Exception { Connection connection = dataSource.getConnection(); + connection.setAutoCommit(false); try { T value = aCallback.execute(connection); connection.commit(); @@ -192,21 +230,26 @@ public class DatabaseUtils { } public void dropTables() throws Exception { - executeOnTables(tables, new TableSetOperation() { - - public void execute(String aTable) throws Exception { - dropTable(aTable); - } - }); + dropTables(tables); } public void dropTables(ITableFilterSimple aTables) throws Exception { - executeOnTables(aTables, new TableSetOperation() { + final String[] tables = getTableNames(aTables); + String[] sortedTables = executeInTransaction(new JdbcUnitOfWork() { - public void execute(String aTable) throws Exception { - dropTable(aTable); + public String[] execute(Connection aConnection) throws Exception { + IDatabaseConnection connection = new DatabaseConnection( + aConnection); + ITableFilter filter = new DatabaseSequenceFilter(connection, + tables); + IDataSet dataset = new FilteredDataSet(filter, connection + .createDataSet(tables)); + return dataset.getTableNames(); } }); + for (int i = sortedTables.length - 1; i >= 0; i--) { + dropTable(sortedTables[i]); + } } /**