Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/backend/utils/DBCPConnectionProvider.java @ 8763:8179cca1796a
Upgrade to DBCP 2.1.1
DBCP 2 needs at least Java 7. We are no longer compatible with Java 6.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Fri, 14 Aug 2015 18:24:04 +0200 |
parents | d718edbf0b0c |
children | 19fa9fc39fa2 |
comparison
equal
deleted
inserted
replaced
8762:86da84a95fac | 8763:8179cca1796a |
---|---|
27 import java.sql.SQLException; | 27 import java.sql.SQLException; |
28 | 28 |
29 import java.util.Iterator; | 29 import java.util.Iterator; |
30 import java.util.Properties; | 30 import java.util.Properties; |
31 import java.util.Map; | 31 import java.util.Map; |
32 import java.util.Collections; | 32 import java.util.Arrays; |
33 import java.util.StringTokenizer; | 33 |
34 | 34 import org.apache.commons.dbcp2.BasicDataSource; |
35 import org.apache.commons.dbcp.BasicDataSource; | 35 import org.apache.commons.dbcp2.BasicDataSourceFactory; |
36 import org.apache.commons.dbcp.BasicDataSourceFactory; | |
37 | 36 |
38 import org.apache.log4j.Logger; | 37 import org.apache.log4j.Logger; |
39 | 38 |
40 import org.hibernate.HibernateException; | 39 import org.hibernate.HibernateException; |
41 | 40 |
206 | 205 |
207 // This needs to be done manually as it is somehow ignored | 206 // This needs to be done manually as it is somehow ignored |
208 // by the BasicDataSourceFactory if you set it as a dbcpProperty | 207 // by the BasicDataSourceFactory if you set it as a dbcpProperty |
209 String connectionInitSqls = props.getProperty("connectionInitSqls"); | 208 String connectionInitSqls = props.getProperty("connectionInitSqls"); |
210 if (connectionInitSqls != null) { | 209 if (connectionInitSqls != null) { |
211 StringTokenizer tokenizer = new StringTokenizer(connectionInitSqls, ";"); | 210 String[] statements = connectionInitSqls.split(";"); |
212 ds.setConnectionInitSqls(Collections.list(tokenizer)); | 211 ds.setConnectionInitSqls(Arrays.asList(statements)); |
213 } | 212 } |
214 | 213 |
215 String validationQuery = props.getProperty("validationQuery"); | 214 String validationQuery = props.getProperty("validationQuery"); |
216 if (validationQuery != null) { | 215 if (validationQuery != null) { |
217 ds.setValidationQuery(validationQuery); | 216 ds.setValidationQuery(validationQuery); |
218 } | 217 } |
219 | 218 |
220 String maxWait = props.getProperty("maxWait"); | 219 String maxWait = props.getProperty("maxWait"); |
221 if (maxWait != null) { | 220 if (maxWait != null) { |
222 try { | 221 try { |
223 ds.setMaxWait(Integer.parseInt(maxWait)); | 222 ds.setMaxWaitMillis(Integer.parseInt(maxWait)); |
224 } | 223 } |
225 catch (NumberFormatException nfe) { | 224 catch (NumberFormatException nfe) { |
226 log.error( | 225 log.error( |
227 "Property maxWait could not be parsed as integer." | 226 "Property maxWait could not be parsed as integer." |
228 ); | 227 ); |
254 | 253 |
255 public Connection getConnection() throws SQLException { | 254 public Connection getConnection() throws SQLException { |
256 log.trace("Connection pool parameters:"); | 255 log.trace("Connection pool parameters:"); |
257 log.trace("_ active connections: " + ds.getNumActive()); | 256 log.trace("_ active connections: " + ds.getNumActive()); |
258 log.trace("_ idle connections: " + ds.getNumIdle()); | 257 log.trace("_ idle connections: " + ds.getNumIdle()); |
259 log.trace("_ max active: " + ds.getMaxActive()); | 258 log.trace("_ max active: " + ds.getMaxTotal()); |
260 if (ds.getNumActive() == ds.getMaxActive()) { | 259 if (ds.getNumActive() == ds.getMaxTotal()) { |
261 log.warn("Maximum number of database connections in pool in use!"); | 260 log.warn("Maximum number of database connections in pool in use!"); |
262 } | 261 } |
263 Connection conn = ds.getConnection(); | 262 Connection conn = ds.getConnection(); |
264 log.trace("Return connection with hash: " + conn.hashCode()); | 263 log.trace("Return connection with hash: " + conn.hashCode()); |
265 return conn; | 264 return conn; |