Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DB6C7200B98 for ; Mon, 3 Oct 2016 08:35:54 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id CF43E160ADC; Mon, 3 Oct 2016 06:35:54 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id ED67F160ACC for ; Mon, 3 Oct 2016 08:35:53 +0200 (CEST) Received: (qmail 78830 invoked by uid 500); 3 Oct 2016 06:35:53 -0000 Mailing-List: contact commits-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list commits@cayenne.apache.org Received: (qmail 78821 invoked by uid 99); 3 Oct 2016 06:35:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Oct 2016 06:35:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id CC3C7DFD9F; Mon, 3 Oct 2016 06:35:52 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aadamchik@apache.org To: commits@cayenne.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cayenne git commit: JDBC resource leak - fixing... Date: Mon, 3 Oct 2016 06:35:52 +0000 (UTC) archived-at: Mon, 03 Oct 2016 06:35:55 -0000 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 Authored: Mon Oct 3 09:35:46 2016 +0300 Committer: Andrus Adamchik 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); } }