cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject cayenne git commit: JDBC resource leak - fixing...
Date Mon, 03 Oct 2016 06:35:52 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master b716b7be7 -> e8e2f733c


JDBC resource leak  - fixing...


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/e8e2f733
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/e8e2f733
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/e8e2f733

Branch: refs/heads/master
Commit: e8e2f733c6c6122d0068c4c40a28d57e9592aa33
Parents: b716b7b
Author: Andrus Adamchik <andrus@objectstyle.com>
Authored: Mon Oct 3 09:35:46 2016 +0300
Committer: Andrus Adamchik <andrus@objectstyle.com>
Committed: Mon Oct 3 09:35:46 2016 +0300

----------------------------------------------------------------------
 .../cayenne/tools/DbImporterMojoTest.java       | 72 +++++++++++---------
 1 file changed, 41 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/e8e2f733/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
----------------------------------------------------------------------
diff --git a/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
b/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
index 775cca2..a736faf 100644
--- a/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
+++ b/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java
@@ -18,12 +18,12 @@
  ****************************************************************/
 package org.apache.cayenne.tools;
 
-import org.apache.cayenne.test.jdbc.SQLReader;
-import org.apache.cayenne.test.resource.ResourceUtil;
-import org.apache.cayenne.tools.dbimport.DbImportConfiguration;
 import org.apache.cayenne.dbimport.Catalog;
 import org.apache.cayenne.dbimport.IncludeTable;
 import org.apache.cayenne.dbimport.Schema;
+import org.apache.cayenne.test.jdbc.SQLReader;
+import org.apache.cayenne.test.resource.ResourceUtil;
+import org.apache.cayenne.tools.dbimport.DbImportConfiguration;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.codehaus.plexus.util.FileUtils;
@@ -315,37 +315,47 @@ public class DbImporterMojoTest extends AbstractMojoTestCase {
 
     private void cleanDb(DbImportConfiguration dbImportConfiguration) throws ClassNotFoundException,
             IllegalAccessException, InstantiationException, SQLException {
+
+        // TODO: refactor to common DB management code... E.g. bootique-jdbc-test?
+        // TODO: with in-memory Derby, it is easier to just stop and delete the database..
again see bootique-jdbc-test
+
         Class.forName(dbImportConfiguration.getDriver()).newInstance();
-        // Get a connection
-        Connection connection = DriverManager.getConnection(dbImportConfiguration.getUrl());
-        Statement stmt = connection.createStatement();
-
-        ResultSet views = connection.getMetaData().getTables(null, null, null, new String[]{"VIEW"});
-        while (views.next()) {
-            String schema = views.getString("TABLE_SCHEM");
-            execute(stmt, "DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME"));
-        }
 
-        ResultSet tables = connection.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
-        while (tables.next()) {
-            String schema = tables.getString("TABLE_SCHEM");
-            String tableName = tables.getString("TABLE_NAME");
-            String tableNameFull = (isBlank(schema) ? "" : schema + ".") + tableName;
+        try (Connection connection = DriverManager.getConnection(dbImportConfiguration.getUrl()))
{
 
-            ResultSet keys = connection.getMetaData().getExportedKeys(null, schema, tableName);
-            while (keys.next()) {
-                execute(stmt, "ALTER TABLE " + keys.getString("FKTABLE_NAME") + " DROP CONSTRAINT
" + keys.getString("FK_NAME"));
-            }
+            try (Statement stmt = connection.createStatement();) {
 
-            String sql = "DROP TABLE " + tableNameFull;
-            execute(stmt, sql);
-        }
+                try (ResultSet views = connection.getMetaData().getTables(null, null, null,
new String[]{"VIEW"});) {
+                    while (views.next()) {
+                        String schema = views.getString("TABLE_SCHEM");
+                        execute(stmt, "DROP VIEW " + (isBlank(schema) ? "" : schema + ".")
+ views.getString("TABLE_NAME"));
+                    }
+                }
 
-        ResultSet schemas = connection.getMetaData().getSchemas();
-        while (schemas.next()) {
-            String schem = schemas.getString("TABLE_SCHEM");
-            if (schem.startsWith("SCHEMA")) {
-                execute(stmt, "DROP SCHEMA " + schem + " RESTRICT");
+                try (ResultSet tables = connection.getMetaData().getTables(null, null, null,
new String[]{"TABLE"});) {
+                    while (tables.next()) {
+                        String schema = tables.getString("TABLE_SCHEM");
+                        String tableName = tables.getString("TABLE_NAME");
+                        String tableNameFull = (isBlank(schema) ? "" : schema + ".") + tableName;
+
+                        ResultSet keys = connection.getMetaData().getExportedKeys(null, schema,
tableName);
+                        while (keys.next()) {
+                            execute(stmt, "ALTER TABLE " + keys.getString("FKTABLE_NAME")
+ " DROP CONSTRAINT " + keys.getString("FK_NAME"));
+                        }
+
+                        String sql = "DROP TABLE " + tableNameFull;
+                        execute(stmt, sql);
+                    }
+                }
+
+                try (ResultSet schemas = connection.getMetaData().getSchemas();) {
+                    while (schemas.next()) {
+                        String schem = schemas.getString("TABLE_SCHEM");
+                        if (schem.startsWith("SCHEMA")) {
+                            execute(stmt, "DROP SCHEMA " + schem + " RESTRICT");
+                        }
+                    }
+                }
             }
         }
     }
@@ -378,7 +388,7 @@ public class DbImporterMojoTest extends AbstractMojoTestCase {
 
     private void prepareDatabase(String sqlFile, DbImportConfiguration dbImportConfiguration)
throws Exception {
 
-        URL dbUrl = Objects.requireNonNull(ResourceUtil.getResource(getClass(), "dbimport/"
+ sqlFile + ".sql"));
+        URL sqlUrl = Objects.requireNonNull(ResourceUtil.getResource(getClass(), "dbimport/"
+ sqlFile + ".sql"));
 
         // TODO: refactor to common DB management code... E.g. bootique-jdbc-test?
 
@@ -386,7 +396,7 @@ public class DbImporterMojoTest extends AbstractMojoTestCase {
 
         try (Connection connection = DriverManager.getConnection(dbImportConfiguration.getUrl()))
{
             try (Statement stmt = connection.createStatement();) {
-                for (String sql : SQLReader.statements(dbUrl, ";")) {
+                for (String sql : SQLReader.statements(sqlUrl, ";")) {
                     stmt.execute(sql);
                 }
             }


Mime
View raw message