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 2015/12/01 19:58:18 UTC

incubator-groovy git commit: GROOVY-7673: Remove synchronized methods of groovy.sql.Sql and document it as not thread-safe

Repository: incubator-groovy
Updated Branches:
  refs/heads/master 47db5a143 -> 103519a38


GROOVY-7673: Remove synchronized methods of groovy.sql.Sql and document it as not thread-safe


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

Branch: refs/heads/master
Commit: 103519a38826ee5fe27d832d0086571c2c89a429
Parents: 47db5a1
Author: pascalschumacher <pa...@gmx.net>
Authored: Tue Dec 1 19:58:04 2015 +0100
Committer: pascalschumacher <pa...@gmx.net>
Committed: Tue Dec 1 19:58:04 2015 +0100

----------------------------------------------------------------------
 .../src/main/java/groovy/sql/Sql.java           | 34 +++++++++-----------
 1 file changed, 15 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/103519a3/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 e49c029..3af3023 100644
--- a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
+++ b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
@@ -218,6 +218,8 @@ import static org.codehaus.groovy.runtime.SqlGroovyMethods.toRowResult;
  * facade behavior associated with the various aspects of managing
  * the interaction with the underlying database.
  *
+ * This class is <b>not</b> thread-safe.
+ *
  * @author Chris Stevenson
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
  * @author Paul King
@@ -3465,7 +3467,7 @@ public class Sql {
      *
      * @param cacheStatements the new value
      */
-    public synchronized void setCacheStatements(boolean cacheStatements) {
+    public void setCacheStatements(boolean cacheStatements) {
         this.cacheStatements = cacheStatements;
         if (!cacheStatements) {
             clearStatementCache();
@@ -3487,7 +3489,7 @@ public class Sql {
      * @param closure the given closure
      * @throws SQLException if a database error occurs
      */
-    public synchronized void cacheConnection(Closure closure) throws SQLException {
+    public void cacheConnection(Closure closure) throws SQLException {
         boolean savedCacheConnection = cacheConnection;
         cacheConnection = true;
         Connection connection = null;
@@ -3512,7 +3514,7 @@ public class Sql {
      * @param closure the given closure
      * @throws SQLException if a database error occurs
      */
-    public synchronized void withTransaction(Closure closure) throws SQLException {
+    public void withTransaction(Closure closure) throws SQLException {
         boolean savedCacheConnection = cacheConnection;
         cacheConnection = true;
         Connection connection = null;
@@ -3831,7 +3833,7 @@ public class Sql {
      * @throws SQLException if a database error occurs
      * @see #setCacheStatements(boolean)
      */
-    public synchronized void cacheStatements(Closure closure) throws SQLException {
+    public void cacheStatements(Closure closure) throws SQLException {
         boolean savedCacheStatements = cacheStatements;
         cacheStatements = true;
         Connection connection = null;
@@ -4312,15 +4314,11 @@ public class Sql {
 
     private void clearStatementCache() {
         Statement statements[];
-        synchronized (statementCache) {
-            if (statementCache.isEmpty())
-                return;
-            // Arrange to call close() outside synchronized block, since
-            // the close may involve server requests.
-            statements = new Statement[statementCache.size()];
-            statementCache.values().toArray(statements);
-            statementCache.clear();
-        }
+        if (statementCache.isEmpty())
+            return;
+        statements = new Statement[statementCache.size()];
+        statementCache.values().toArray(statements);
+        statementCache.clear();
         for (Statement s : statements) {
             try {
                 s.close();
@@ -4336,12 +4334,10 @@ public class Sql {
     private Statement getAbstractStatement(AbstractStatementCommand cmd, Connection connection, String sql) throws SQLException {
         Statement stmt;
         if (cacheStatements) {
-            synchronized (statementCache) { // checking for existence without sync can cause leak if object needs close().
-                stmt = statementCache.get(sql);
-                if (stmt == null) {
-                    stmt = cmd.execute(connection, sql);
-                    statementCache.put(sql, stmt);
-                }
+            stmt = statementCache.get(sql);
+            if (stmt == null) {
+                stmt = cmd.execute(connection, sql);
+                statementCache.put(sql, stmt);
             }
         } else {
             stmt = cmd.execute(connection, sql);