From 72e471f0b44df72d79e8481ae11768c141089569 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 11 Jul 2008 09:52:31 +0000 Subject: [PATCH] Allow subclasses to modify the connection and statement instances used by or to access the cached instances. PR 27178. Submitted by Mike Davis git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@675894 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + WHATSNEW | 5 ++ contributors.xml | 4 + .../apache/tools/ant/taskdefs/SQLExec.java | 77 +++++++++++++------ 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index fe35d00f0..fee2a9338 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -186,6 +186,7 @@ Michael Newcomb Micheal Nygard Michael Saunders Miha +Mike Davis Mike Roberts mnowostawski Nick Chalko diff --git a/WHATSNEW b/WHATSNEW index 2644bdd46..f1dc60d35 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -115,6 +115,11 @@ Other changes: running on OpenJDK. Bugzilla Report 44889. + * new protected getConnection and getStatement methods allow + subclasses of SQLExec more control - or access to the cached + instances when overriding other methods like runStatements. + Bugzilla Report 27178. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/contributors.xml b/contributors.xml index e0878f961..9e37b16c2 100644 --- a/contributors.xml +++ b/contributors.xml @@ -759,6 +759,10 @@ Miha + + Mike + Davis + Mike Roberts diff --git a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java index 708fbe34f..53b315319 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SQLExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/SQLExec.java @@ -435,14 +435,8 @@ public class SQLExec extends JDBCTask { Transaction t = createTransaction(); t.setSrc(srcFile); t.addText(sqlCommand); - conn = getConnection(); - if (!isValidRdbms(conn)) { - return; - } - try { - statement = conn.createStatement(); - statement.setEscapeProcessing(escapeProcessing); + try { PrintStream out = System.out; try { if (output != null) { @@ -458,7 +452,7 @@ public class SQLExec extends JDBCTask { ((Transaction) e.nextElement()).runTransaction(out); if (!isAutocommit()) { log("Committing transaction", Project.MSG_VERBOSE); - conn.commit(); + getConnection().commit(); } } } finally { @@ -476,15 +470,15 @@ public class SQLExec extends JDBCTask { } } finally { try { - if (statement != null) { - statement.close(); + if (getStatement() != null) { + getStatement().close(); } } catch (SQLException ex) { // ignore } try { - if (conn != null) { - conn.close(); + if (getConnection() != null) { + getConnection().close(); } } catch (SQLException ex) { // ignore @@ -575,9 +569,9 @@ public class SQLExec extends JDBCTask { boolean ret; int updateCount = 0, updateCountTotal = 0; - ret = statement.execute(sql); - updateCount = statement.getUpdateCount(); - resultSet = statement.getResultSet(); + ret = getStatement().execute(sql); + updateCount = getStatement().getUpdateCount(); + resultSet = getStatement().getResultSet(); do { if (!ret) { if (updateCount != -1) { @@ -586,10 +580,10 @@ public class SQLExec extends JDBCTask { } else if (print) { printResults(resultSet, out); } - ret = statement.getMoreResults(); + ret = getStatement().getMoreResults(); if (ret) { - updateCount = statement.getUpdateCount(); - resultSet = statement.getResultSet(); + updateCount = getStatement().getUpdateCount(); + resultSet = getStatement().getResultSet(); } } while (ret); @@ -598,12 +592,12 @@ public class SQLExec extends JDBCTask { if (print && showtrailers) { out.println(updateCountTotal + " rows affected"); } - SQLWarning warning = conn.getWarnings(); + SQLWarning warning = getConnection().getWarnings(); while (warning != null) { log(warning + " sql warning", Project.MSG_VERBOSE); warning = warning.getNextWarning(); } - conn.clearWarnings(); + getConnection().clearWarnings(); goodSql++; } catch (SQLException e) { log("Failed to execute: " + sql, Project.MSG_ERR); @@ -633,7 +627,7 @@ public class SQLExec extends JDBCTask { * @throws SQLException on SQL problems. */ protected void printResults(PrintStream out) throws SQLException { - ResultSet rs = statement.getResultSet(); + ResultSet rs = getStatement().getResultSet(); try { printResults(rs, out); } finally { @@ -692,15 +686,52 @@ public class SQLExec extends JDBCTask { * @since Ant 1.7 */ private void closeQuietly() { - if (!isAutocommit() && conn != null && onError.equals("abort")) { + if (!isAutocommit() && getConnection() != null && onError.equals("abort")) { try { - conn.rollback(); + getConnection().rollback(); } catch (SQLException ex) { // ignore } } } + + /** + * Caches the connection returned by the base class's getConnection method. + * + *

Subclasses that need to provide a different connection than + * the base class would, should override this method but keep in + * mind that this class expects to get the same connection + * instance on consecutive calls.

+ */ + protected Connection getConnection() { + if (conn == null) { + conn = super.getConnection(); + if (!isValidRdbms(conn)) { + conn = null; + } + } + return conn; + } + + /** + * Creates and configures a Statement instance which is then + * cached for subsequent calls. + * + *

Subclasses that want to provide different Statement + * instances, should override this method but keep in mind that + * this class expects to get the same connection instance on + * consecutive calls.

+ */ + protected Statement getStatement() throws SQLException { + if (statement == null) { + statement = getConnection().createStatement(); + statement.setEscapeProcessing(escapeProcessing); + } + + return statement; + } + /** * The action a task should perform on an error, * one of "continue", "stop" and "abort"