groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [1/2] groovy git commit: Ensure BatchingStatementWrapper is closed when statement caching is enabled
Date Fri, 11 Mar 2016 02:56:35 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 778cb5432 -> 9fca80db4


Ensure BatchingStatementWrapper is closed when statement caching is enabled


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 9fca80db49a390880e1fde033b5e4c4263edde3d
Parents: 77ee2fe
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Thu Mar 10 18:28:53 2016 -0800
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu Mar 10 18:40:05 2016 -0800

----------------------------------------------------------------------
 .../src/main/java/groovy/sql/Sql.java           |  6 +++-
 .../test/groovy/groovy/sql/SqlBatchTest.groovy  | 34 ++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9fca80db/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
----------------------------------------------------------------------
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 a3f2528..1fb05f2 100644
--- a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
+++ b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
@@ -4221,8 +4221,12 @@ public class Sql {
         closeResources(connection);
     }
 
-    private void closeResources(BatchingStatementWrapper statement) {
+    private void closeResources(BatchingPreparedStatementWrapper statement) {
         if (cacheStatements) return;
+        closeResources((BatchingStatementWrapper) statement);
+    }
+
+    private void closeResources(BatchingStatementWrapper statement) {
         if (statement != null) {
             try {
                 statement.close();

http://git-wip-us.apache.org/repos/asf/groovy/blob/9fca80db/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy b/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy
index 3f9bafd..02a6d65 100644
--- a/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy
+++ b/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy
@@ -171,4 +171,38 @@ class SqlBatchTest extends GroovyTestCase {
         // FINE: Successfully executed batch with 1 command(s)
     }
 
+    void testWithBatchClosesStatement() {
+        // withBatch(int, Closure) should close connection even when statement caching is
enabled
+        // since it is not cached because there is no sql text to use as the cache key.
+        BatchingStatementWrapper wrapper = null
+        sql.cacheStatements = true
+        sql.withBatch(1) { stmt ->
+            wrapper = (BatchingStatementWrapper)stmt
+            stmt.addBatch("insert into PERSON (id, firstname, lastname) values (999, 'Test',
'Closes')")
+        }
+        assert wrapper.@delegate.isClosed()
+    }
+
+    void testWithBatchClosureClosesOrCachesStatement() {
+        String sqlText = 'insert into PERSON (id, firstname, lastname) values (?, ?, ?)'
+        BatchingPreparedStatementWrapper wrapper = null
+
+        sql.cacheStatements = false
+        sql.withBatch(20, sqlText) { ps ->
+            wrapper = (BatchingPreparedStatementWrapper)ps
+            ps.addBatch(111, 'Test1', 'Closes1')
+            ps.addBatch(222, 'Test2', 'Closes2')
+        }
+        assert wrapper.@delegate.isClosed()
+
+        sql.cacheStatements = true
+        sql.withBatch(20, sqlText) { ps ->
+            wrapper = (BatchingPreparedStatementWrapper)ps
+            ps.addBatch(333, 'Test3', 'Closes3')
+            ps.addBatch(444, 'Test4', 'Closes4')
+        }
+        assert !wrapper.@delegate.isClosed()
+        assert sql.@statementCache.containsKey(sqlText)
+        assert sql.@statementCache[sqlText].is(wrapper.@delegate)
+    }
 }


Mime
View raw message