[PATCH] Added R/W lock to SQLExecutor

Wald Commits scm-commit at wald.intevation.org
Thu Sep 3 11:46:38 CEST 2015


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1441273584 -7200
# Node ID 91b1435fb9eaaa9b342886df1e7b275ddc92621c
# Parent  c14bf6b35fc5dd83b88ba0dd8b7064d6052eb207
Added R/W lock to SQLExecutor.

diff -r c14bf6b35fc5 -r 91b1435fb9ea artifact-database/src/main/java/org/dive4elements/artifactdatabase/db/SQLExecutor.java
--- a/artifact-database/src/main/java/org/dive4elements/artifactdatabase/db/SQLExecutor.java	Thu May 07 09:53:37 2015 +0200
+++ b/artifact-database/src/main/java/org/dive4elements/artifactdatabase/db/SQLExecutor.java	Thu Sep 03 11:46:24 2015 +0200
@@ -5,6 +5,8 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
 import javax.sql.DataSource;
 
 import org.apache.log4j.Logger;
@@ -54,40 +56,52 @@
         }
 
         public boolean runWrite() {
-            DataSource dataSource = dbConnection.getDataSource();
+            rwLock.writeLock().lock();
             try {
-                conn = dataSource.getConnection();
+                DataSource dataSource = dbConnection.getDataSource();
                 try {
-                    conn.setAutoCommit(false);
+                    conn = dataSource.getConnection();
+                    try {
+                        conn.setAutoCommit(false);
+                        return doIt();
+                    }
+                    catch (SQLException sqle) {
+                        conn.rollback();
+                        throw sqle;
+                    }
+                }
+                catch (SQLException sqle) {
+                    logger.error(sqle.getLocalizedMessage(), sqle);
+                }
+                finally {
+                    close();
+                }
+                return false;
+            }
+            finally {
+                rwLock.writeLock().unlock();
+            }
+        }
+
+        public boolean runRead() {
+            rwLock.readLock().lock();
+            try {
+                DataSource dataSource = dbConnection.getDataSource();
+                try {
+                    conn = dataSource.getConnection();
                     return doIt();
                 }
                 catch (SQLException sqle) {
-                    conn.rollback();
-                    throw sqle;
+                    logger.error(sqle.getLocalizedMessage(), sqle);
                 }
-            }
-            catch (SQLException sqle) {
-                logger.error(sqle.getLocalizedMessage(), sqle);
+                finally {
+                    close();
+                }
+                return false;
             }
             finally {
-                close();
+                rwLock.readLock().unlock();
             }
-            return false;
-        }
-
-        public boolean runRead() {
-            DataSource dataSource = dbConnection.getDataSource();
-            try {
-                conn = dataSource.getConnection();
-                return doIt();
-            }
-            catch (SQLException sqle) {
-                logger.error(sqle.getLocalizedMessage(), sqle);
-            }
-            finally {
-                close();
-            }
-            return false;
         }
 
         public boolean doIt() throws SQLException {
@@ -95,12 +109,15 @@
         }
     } // class Instance
 
-    protected DBConnection dbConnection;
+    protected DBConnection           dbConnection;
+    protected ReentrantReadWriteLock rwLock;
 
     public SQLExecutor() {
+        rwLock = new ReentrantReadWriteLock();
     }
 
     public SQLExecutor(DBConnection dbConnection) {
+        this();
         this.dbConnection = dbConnection;
     }
 


More information about the Dive4Elements-commits mailing list