Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/db/SQLExecutor.java @ 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 | 415df0fc4fa1 |
children | 3b1e48d22ce0 |
comparison
equal
deleted
inserted
replaced
539:c14bf6b35fc5 | 540:91b1435fb9ea |
---|---|
2 | 2 |
3 import java.sql.Connection; | 3 import java.sql.Connection; |
4 import java.sql.PreparedStatement; | 4 import java.sql.PreparedStatement; |
5 import java.sql.ResultSet; | 5 import java.sql.ResultSet; |
6 import java.sql.SQLException; | 6 import java.sql.SQLException; |
7 | |
8 import java.util.concurrent.locks.ReentrantReadWriteLock; | |
7 | 9 |
8 import javax.sql.DataSource; | 10 import javax.sql.DataSource; |
9 | 11 |
10 import org.apache.log4j.Logger; | 12 import org.apache.log4j.Logger; |
11 | 13 |
52 catch (SQLException sqle) {} | 54 catch (SQLException sqle) {} |
53 } | 55 } |
54 } | 56 } |
55 | 57 |
56 public boolean runWrite() { | 58 public boolean runWrite() { |
57 DataSource dataSource = dbConnection.getDataSource(); | 59 rwLock.writeLock().lock(); |
58 try { | 60 try { |
59 conn = dataSource.getConnection(); | 61 DataSource dataSource = dbConnection.getDataSource(); |
60 try { | 62 try { |
61 conn.setAutoCommit(false); | 63 conn = dataSource.getConnection(); |
64 try { | |
65 conn.setAutoCommit(false); | |
66 return doIt(); | |
67 } | |
68 catch (SQLException sqle) { | |
69 conn.rollback(); | |
70 throw sqle; | |
71 } | |
72 } | |
73 catch (SQLException sqle) { | |
74 logger.error(sqle.getLocalizedMessage(), sqle); | |
75 } | |
76 finally { | |
77 close(); | |
78 } | |
79 return false; | |
80 } | |
81 finally { | |
82 rwLock.writeLock().unlock(); | |
83 } | |
84 } | |
85 | |
86 public boolean runRead() { | |
87 rwLock.readLock().lock(); | |
88 try { | |
89 DataSource dataSource = dbConnection.getDataSource(); | |
90 try { | |
91 conn = dataSource.getConnection(); | |
62 return doIt(); | 92 return doIt(); |
63 } | 93 } |
64 catch (SQLException sqle) { | 94 catch (SQLException sqle) { |
65 conn.rollback(); | 95 logger.error(sqle.getLocalizedMessage(), sqle); |
66 throw sqle; | |
67 } | 96 } |
68 } | 97 finally { |
69 catch (SQLException sqle) { | 98 close(); |
70 logger.error(sqle.getLocalizedMessage(), sqle); | 99 } |
100 return false; | |
71 } | 101 } |
72 finally { | 102 finally { |
73 close(); | 103 rwLock.readLock().unlock(); |
74 } | 104 } |
75 return false; | |
76 } | |
77 | |
78 public boolean runRead() { | |
79 DataSource dataSource = dbConnection.getDataSource(); | |
80 try { | |
81 conn = dataSource.getConnection(); | |
82 return doIt(); | |
83 } | |
84 catch (SQLException sqle) { | |
85 logger.error(sqle.getLocalizedMessage(), sqle); | |
86 } | |
87 finally { | |
88 close(); | |
89 } | |
90 return false; | |
91 } | 105 } |
92 | 106 |
93 public boolean doIt() throws SQLException { | 107 public boolean doIt() throws SQLException { |
94 return true; | 108 return true; |
95 } | 109 } |
96 } // class Instance | 110 } // class Instance |
97 | 111 |
98 protected DBConnection dbConnection; | 112 protected DBConnection dbConnection; |
113 protected ReentrantReadWriteLock rwLock; | |
99 | 114 |
100 public SQLExecutor() { | 115 public SQLExecutor() { |
116 rwLock = new ReentrantReadWriteLock(); | |
101 } | 117 } |
102 | 118 |
103 public SQLExecutor(DBConnection dbConnection) { | 119 public SQLExecutor(DBConnection dbConnection) { |
120 this(); | |
104 this.dbConnection = dbConnection; | 121 this.dbConnection = dbConnection; |
105 } | 122 } |
106 | 123 |
107 public DBConnection getDBConnection() { | 124 public DBConnection getDBConnection() { |
108 return dbConnection; | 125 return dbConnection; |