DBUTILS-143 Use try-with-resources for all prepareConnection calls
Remove closing of connection by private methods that are wrapped in convience methods
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 470d14b..165c52c 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -13,7 +13,8 @@
New features:
o PR/9: Add @Column annotation to hint the field name instead of dissecting the get method name. Thanks to rewerma.
-o DBUTILS-136: CaseInsensitiveHashMap cannot be accessed by subclasses of BasicRowProcessor; add org.apache.commons.dbutils.BasicRowProcessor.createCaseInsensitiveHashMap(int). Thanks to Matthew Hall, Gary Gregory.
+o DBUTILS-143: Only close connection if created in query runners. Thanks to thecarlhall.
+o DBUTILS-136: CaseInsensitiveHashMap cannot be accessed by subclasses of BasicRowProcessor; add org.apache.commons.dbutils.BasicRowProcessor.createCaseInsensitiveHashMap(int). Thanks to Matthew Hall, Gary Gregory.
Fixed Bugs:
o Always copy Date, Time, Timestamp on get and set in SqlNullCheckedResultSet.
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a1dfb35..5d1e2ca 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,6 +45,9 @@
<body>
<release version="1.8" date="2020-01-10" description="New features and bug fixes.">
+ <action dev="thecarlhall" type="fix" issue="DBUTILS-143">
+ Methods in QueryRunner and AsyncQueryRunner only close connections if they create them.
+ </action>
<action dev="thecarlhall" type="fix" issue="DBUTILS-131" due-to="yairlenga">
Speedup query calls without parameters; Use PreparedStatement only when parameters are present.
</action>
diff --git a/src/main/java/org/apache/commons/dbutils/QueryRunner.java b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
index f76ce19..6c062f1 100644
--- a/src/main/java/org/apache/commons/dbutils/QueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
@@ -146,9 +146,9 @@
* @since DbUtils 1.1
*/
public int[] batch(final String sql, final Object[][] params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.batch(conn, true, sql, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.batch(conn, true, sql, params);
+ }
}
/**
@@ -167,16 +167,10 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
if (params == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");
}
@@ -195,9 +189,6 @@
this.rethrow(e, sql, (Object[])params);
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return rows;
@@ -282,9 +273,9 @@
*/
@Deprecated
public <T> T query(final String sql, final Object param, final ResultSetHandler<T> rsh) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, param);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, true, sql, rsh, param);
+ }
}
/**
@@ -305,9 +296,9 @@
*/
@Deprecated
public <T> T query(final String sql, final Object[] params, final ResultSetHandler<T> rsh) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, true, sql, rsh, params);
+ }
}
/**
@@ -324,9 +315,9 @@
* @throws SQLException if a database access error occurs
*/
public <T> T query(final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, true, sql, rsh, params);
+ }
}
/**
@@ -342,9 +333,9 @@
* @throws SQLException if a database access error occurs
*/
public <T> T query(final String sql, final ResultSetHandler<T> rsh) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.<T>query(conn, true, sql, rsh, (Object[]) null);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.<T>query(conn, true, sql, rsh, (Object[]) null);
+ }
}
/**
@@ -364,16 +355,10 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
if (rsh == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null ResultSetHandler");
}
@@ -399,9 +384,6 @@
} finally {
closeQuietly(rs);
closeQuietly(stmt);
- if (closeConn) {
- close(conn);
- }
}
return result;
@@ -459,9 +441,9 @@
* @return The number of rows updated.
*/
public int update(final String sql) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.update(conn, true, sql, (Object[]) null);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.update(conn, true, sql, (Object[]) null);
+ }
}
/**
@@ -477,9 +459,9 @@
* @return The number of rows updated.
*/
public int update(final String sql, final Object param) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.update(conn, true, sql, param);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.update(conn, true, sql, param);
+ }
}
/**
@@ -495,9 +477,9 @@
* @return The number of rows updated.
*/
public int update(final String sql, final Object... params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.update(conn, true, sql, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.update(conn, true, sql, params);
+ }
}
/**
@@ -516,9 +498,6 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
@@ -541,9 +520,6 @@
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return rows;
@@ -562,7 +538,9 @@
* @since 1.6
*/
public <T> T insert(final String sql, final ResultSetHandler<T> rsh) throws SQLException {
- return insert(this.prepareConnection(), true, sql, rsh, (Object[]) null);
+ try (final Connection conn = this.prepareConnection()) {
+ return insert(conn, true, sql, rsh, (Object[]) null);
+ }
}
/**
@@ -580,7 +558,9 @@
* @since 1.6
*/
public <T> T insert(final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- return insert(this.prepareConnection(), true, sql, rsh, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return insert(conn, true, sql, rsh, params);
+ }
}
/**
@@ -633,16 +613,10 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
if (rsh == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null ResultSetHandler");
}
@@ -665,9 +639,6 @@
this.rethrow(e, sql, params);
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return generatedKeys;
@@ -688,7 +659,9 @@
* @since 1.6
*/
public <T> T insertBatch(final String sql, final ResultSetHandler<T> rsh, final Object[][] params) throws SQLException {
- return insertBatch(this.prepareConnection(), true, sql, rsh, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return insertBatch(conn, true, sql, rsh, params);
+ }
}
/**
@@ -726,16 +699,10 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
if (params == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");
}
@@ -756,9 +723,6 @@
this.rethrow(e, sql, (Object[])params);
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return generatedKeys;
@@ -810,9 +774,9 @@
* @return The number of rows updated.
*/
public int execute(final String sql, final Object... params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.execute(conn, true, sql, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.execute(conn, true, sql, params);
+ }
}
/**
@@ -863,9 +827,9 @@
* @throws SQLException if a database access error occurs
*/
public <T> List<T> execute(final String sql, final ResultSetHandler<T> rsh, final Object... params) throws SQLException {
- final Connection conn = this.prepareConnection();
-
- return this.execute(conn, true, sql, rsh, params);
+ try (final Connection conn = this.prepareConnection()) {
+ return this.execute(conn, true, sql, rsh, params);
+ }
}
/**
@@ -885,9 +849,6 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
@@ -906,9 +867,6 @@
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return rows;
@@ -932,16 +890,10 @@
}
if (sql == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null SQL statement");
}
if (rsh == null) {
- if (closeConn) {
- close(conn);
- }
throw new SQLException("Null ResultSetHandler");
}
@@ -972,9 +924,6 @@
} finally {
close(stmt);
- if (closeConn) {
- close(conn);
- }
}
return results;
diff --git a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java b/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
index 2802c16..c010858 100644
--- a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
+++ b/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
@@ -46,7 +46,6 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
-import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@@ -645,7 +644,7 @@
verify(call, times(1)).execute();
verify(call, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we do not close the connection
+ verify(conn, times(1)).close(); // make sure we closed the connection
// call the other variation of query
when(meta.getParameterCount()).thenReturn(0);
@@ -655,7 +654,7 @@
verify(call, times(2)).execute();
verify(call, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we do not close the connection
+ verify(conn, times(2)).close(); // make sure we closed the connection
// Test single OUT parameter
when(meta.getParameterCount()).thenReturn(1);
@@ -669,7 +668,7 @@
verify(call, times(3)).execute();
verify(call, times(3)).close(); // make sure we closed the statement
- verify(conn, times(3)).close(); // make sure we do not close the connection
+ verify(conn, times(3)).close(); // make sure we closed the connection
// Test OUT parameters with IN parameters
when(meta.getParameterCount()).thenReturn(3);
@@ -682,7 +681,7 @@
verify(call, times(4)).execute();
verify(call, times(4)).close(); // make sure we closed the statement
- verify(conn, times(4)).close(); // make sure we do not close the connection
+ verify(conn, times(4)).close(); // make sure we closed the connection
// Test INOUT parameters
when(meta.getParameterCount()).thenReturn(3);
@@ -699,7 +698,7 @@
verify(call, times(5)).execute();
verify(call, times(5)).close(); // make sure we closed the statement
- verify(conn, times(5)).close(); // make sure we do not close the connection
+ verify(conn, times(5)).close(); // make sure we closed the connection
}
@Test