Now setting connection autocommit to false before
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / DatabaseUtils.java
index bc62274914081d199aa9cd427cd0943bca2b5433..d2abd90aeccd66eb12331370ed11602e202b9e27 100644 (file)
@@ -12,7 +12,7 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.wamblee.support.persistence;
 
 import java.sql.Connection;
@@ -90,15 +90,12 @@ public class DatabaseUtils {
         final String[] tables = getTableNames(aTables);
         executeInTransaction(new JdbcUnitOfWork<Void>() {
             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 +120,7 @@ public class DatabaseUtils {
     public <T> T executeInTransaction(JdbcUnitOfWork<T> aCallback)
         throws Exception {
         Connection connection = dataSource.getConnection();
+        connection.setAutoCommit(false);
         try {
             T value = aCallback.execute(connection);
             connection.commit();
@@ -146,15 +144,20 @@ public class DatabaseUtils {
         LOG.fine("Getting database table names to clean (schema: '" +
             SCHEMA_PATTERN + "'");
 
-        ResultSet tables = dataSource.getConnection().getMetaData().getTables(
-            null, SCHEMA_PATTERN, "%", new String[] { "TABLE" });
-        while (tables.next()) {
-            String table = tables.getString("TABLE_NAME");
-            if (aSelection.accept(table)) {
-                result.add(table);
+        Connection connection = dataSource.getConnection();
+        try {
+            ResultSet tables = connection.getMetaData().getTables(null,
+                SCHEMA_PATTERN, "%", new String[] { "TABLE" });
+            while (tables.next()) {
+                String table = tables.getString("TABLE_NAME");
+                if (aSelection.accept(table)) {
+                    result.add(table);
+                }
             }
+            return (String[]) result.toArray(new String[0]);
+        } finally {
+            connection.close();
         }
-        return (String[]) result.toArray(new String[0]);
     }
 
     public void emptyTables() throws Exception {
@@ -185,25 +188,30 @@ public class DatabaseUtils {
     public void emptyTable(String aTable) throws Exception {
         executeSql("delete from " + aTable);
     }
-
-    public void dropTables() throws Exception {
-        executeOnTables(tables, new TableSetOperation() {
-
-            public void execute(String aTable) throws Exception {
-                dropTable(aTable);
-            }
-        });
+    
+    public void dropTables() throws Exception { 
+        dropTables(tables);
     }
 
     public void dropTables(ITableFilterSimple aTables) throws Exception {
-        executeOnTables(aTables, new TableSetOperation() {
+        final String[] tables = getTableNames(aTables);
+        String[] sortedTables = executeInTransaction(new JdbcUnitOfWork<String[]>() {
 
-            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]);
+        }
     }
-
+    
     /**
      * @return
      * @throws SQLException
@@ -363,7 +371,7 @@ public class DatabaseUtils {
         if (aObject instanceof Integer) {
             aStatement.setInt(aIndex, ((Integer) aObject).intValue());
         } else if (aObject instanceof Long) {
-            aStatement.setLong(aIndex, ((Integer) aObject).longValue());
+            aStatement.setLong(aIndex, ((Long) aObject).longValue());
         } else if (aObject instanceof String) {
             aStatement.setString(aIndex, (String) aObject);
         } else {