groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject groovy git commit: Revert "GROOVY-7673: Remove synchronized methods of groovy.sql.Sql and document it as not thread-safe"
Date Thu, 03 Dec 2015 23:02:08 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X d57a01cfa -> 55933a30e


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

This reverts commit c62a2667e53de6bf46f4dcc6cbbf43ca4adbb0e8.


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 55933a30e478e8793fea21e1b1f569fdefae1eef
Parents: d57a01c
Author: pascalschumacher <pascalschumacher@gmx.net>
Authored: Fri Dec 4 00:00:57 2015 +0100
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Fri Dec 4 00:01:47 2015 +0100

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


http://git-wip-us.apache.org/repos/asf/groovy/blob/55933a30/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 3af3023..e49c029 100644
--- a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
+++ b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
@@ -218,8 +218,6 @@ 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
@@ -3467,7 +3465,7 @@ public class Sql {
      *
      * @param cacheStatements the new value
      */
-    public void setCacheStatements(boolean cacheStatements) {
+    public synchronized void setCacheStatements(boolean cacheStatements) {
         this.cacheStatements = cacheStatements;
         if (!cacheStatements) {
             clearStatementCache();
@@ -3489,7 +3487,7 @@ public class Sql {
      * @param closure the given closure
      * @throws SQLException if a database error occurs
      */
-    public void cacheConnection(Closure closure) throws SQLException {
+    public synchronized void cacheConnection(Closure closure) throws SQLException {
         boolean savedCacheConnection = cacheConnection;
         cacheConnection = true;
         Connection connection = null;
@@ -3514,7 +3512,7 @@ public class Sql {
      * @param closure the given closure
      * @throws SQLException if a database error occurs
      */
-    public void withTransaction(Closure closure) throws SQLException {
+    public synchronized void withTransaction(Closure closure) throws SQLException {
         boolean savedCacheConnection = cacheConnection;
         cacheConnection = true;
         Connection connection = null;
@@ -3833,7 +3831,7 @@ public class Sql {
      * @throws SQLException if a database error occurs
      * @see #setCacheStatements(boolean)
      */
-    public void cacheStatements(Closure closure) throws SQLException {
+    public synchronized void cacheStatements(Closure closure) throws SQLException {
         boolean savedCacheStatements = cacheStatements;
         cacheStatements = true;
         Connection connection = null;
@@ -4314,11 +4312,15 @@ public class Sql {
 
     private void clearStatementCache() {
         Statement statements[];
-        if (statementCache.isEmpty())
-            return;
-        statements = new Statement[statementCache.size()];
-        statementCache.values().toArray(statements);
-        statementCache.clear();
+        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();
+        }
         for (Statement s : statements) {
             try {
                 s.close();
@@ -4334,10 +4336,12 @@ public class Sql {
     private Statement getAbstractStatement(AbstractStatementCommand cmd, Connection connection,
String sql) throws SQLException {
         Statement stmt;
         if (cacheStatements) {
-            stmt = statementCache.get(sql);
-            if (stmt == null) {
-                stmt = cmd.execute(connection, sql);
-                statementCache.put(sql, stmt);
+            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);
+                }
             }
         } else {
             stmt = cmd.execute(connection, sql);


Mime
View raw message