changeset 540:91b1435fb9ea

Added R/W lock to SQLExecutor.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 03 Sep 2015 11:46:24 +0200
parents c14bf6b35fc5
children 3b1e48d22ce0
files artifact-database/src/main/java/org/dive4elements/artifactdatabase/db/SQLExecutor.java
diffstat 1 files changed, 43 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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;
     }
 

http://dive4elements.wald.intevation.org