You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/02/01 06:52:38 UTC

[groovy] 01/02: Trivial refactoring: handle `Sql` with try-with-resources for simplicity (closes #861)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 9e59f4e731567628199742190a5cef4ae61d42b1
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jan 27 01:08:18 2019 +0800

    Trivial refactoring: handle `Sql` with try-with-resources for simplicity (closes #861)
---
 .../groovy-sql/src/main/java/groovy/sql/Sql.java   | 42 ++++------------------
 1 file changed, 7 insertions(+), 35 deletions(-)

diff --git a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
index 513bc0e..8378288 100644
--- a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
+++ b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
@@ -294,12 +294,8 @@ public class Sql implements AutoCloseable {
      * @throws SQLException if a database access error occurs
      */
     public static void withInstance(String url, Closure c) throws SQLException {
-        Sql sql = null;
-        try {
-            sql = newInstance(url);
+        try (Sql sql = newInstance(url)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }
 
@@ -334,12 +330,8 @@ public class Sql implements AutoCloseable {
      * @throws SQLException if a database access error occurs
      */
     public static void withInstance(String url, Properties properties, Closure c) throws SQLException {
-        Sql sql = null;
-        try {
-            sql = newInstance(url, properties);
+        try (Sql sql = newInstance(url, properties)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }
 
@@ -380,12 +372,8 @@ public class Sql implements AutoCloseable {
      */
     public static void withInstance(String url, Properties properties, String driverClassName, Closure c)
             throws SQLException, ClassNotFoundException {
-        Sql sql = null;
-        try {
-            sql = newInstance(url, properties, driverClassName);
+        try (Sql sql = newInstance(url, properties, driverClassName)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }
 
@@ -420,12 +408,8 @@ public class Sql implements AutoCloseable {
      * @throws SQLException if a database access error occurs
      */
     public static void withInstance(String url, String user, String password, Closure c) throws SQLException {
-        Sql sql = null;
-        try {
-            sql = newInstance(url, user, password);
+        try (Sql sql = newInstance(url, user, password)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }
 
@@ -466,12 +450,8 @@ public class Sql implements AutoCloseable {
      */
     public static void withInstance(String url, String user, String password, String driverClassName, Closure c)
             throws SQLException, ClassNotFoundException {
-        Sql sql = null;
-        try {
-            sql = newInstance(url, user, password, driverClassName);
+        try (Sql sql = newInstance(url, user, password, driverClassName)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }
 
@@ -505,12 +485,8 @@ public class Sql implements AutoCloseable {
      */
     public static void withInstance(String url, String driverClassName, Closure c)
             throws SQLException, ClassNotFoundException {
-        Sql sql = null;
-        try {
-            sql = newInstance(url, driverClassName);
+        try (Sql sql = newInstance(url, driverClassName)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }
 
@@ -622,12 +598,8 @@ public class Sql implements AutoCloseable {
      * @throws ClassNotFoundException if the driver class cannot be found or loaded
      */
     public static void withInstance(Map<String, Object> args, Closure c) throws SQLException, ClassNotFoundException {
-        Sql sql = null;
-        try {
-            sql = newInstance(args);
+        try (Sql sql = newInstance(args)) {
             c.call(sql);
-        } finally {
-            if (sql != null) sql.close();
         }
     }