Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/backend/utils/DBCPConnectionProvider.java @ 8187:3bb1c62ad732
Moved package org.dive4elements.river.utils to org.dive4elements.river.backend.utils.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 04 Sep 2014 15:03:25 +0200 |
parents | backend/src/main/java/org/dive4elements/river/utils/DBCPConnectionProvider.java@4c3ccf2b0304 |
children | c5a7aae52396 |
comparison
equal
deleted
inserted
replaced
8186:a1ceacf15d3a | 8187:3bb1c62ad732 |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 /* | |
10 * Copyright 2004 The Apache Software Foundation. | |
11 * | |
12 * Licensed under the Apache License, Version 2.0 (the "License"); | |
13 * you may not use this file except in compliance with the License. | |
14 * You may obtain a copy of the License at | |
15 * | |
16 * http://www.apache.org/licenses/LICENSE-2.0 | |
17 * | |
18 * Unless required by applicable law or agreed to in writing, software | |
19 * distributed under the License is distributed on an "AS IS" BASIS, | |
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
21 * See the License for the specific language governing permissions and | |
22 * limitations under the License. | |
23 */ | |
24 package org.dive4elements.river.backend.utils; | |
25 | |
26 import java.sql.Connection; | |
27 import java.sql.SQLException; | |
28 | |
29 import java.util.Iterator; | |
30 import java.util.Properties; | |
31 import java.util.Map; | |
32 import java.util.Collections; | |
33 import java.util.StringTokenizer; | |
34 | |
35 import org.apache.commons.dbcp.BasicDataSource; | |
36 import org.apache.commons.dbcp.BasicDataSourceFactory; | |
37 | |
38 import org.apache.log4j.Logger; | |
39 | |
40 import org.hibernate.HibernateException; | |
41 | |
42 import org.hibernate.connection.ConnectionProviderFactory; | |
43 import org.hibernate.connection.ConnectionProvider; | |
44 | |
45 import org.hibernate.cfg.Environment; | |
46 | |
47 /** | |
48 * <p>A connection provider that uses an Apache commons DBCP connection pool.</p> | |
49 * | |
50 * <p>To use this connection provider set:<br> | |
51 * <code>hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider</code></p> | |
52 * | |
53 * <pre>Supported Hibernate properties: | |
54 * hibernate.connection.driver_class | |
55 * hibernate.connection.url | |
56 * hibernate.connection.username | |
57 * hibernate.connection.password | |
58 * hibernate.connection.isolation | |
59 * hibernate.connection.autocommit | |
60 * hibernate.connection.pool_size | |
61 * hibernate.connection (JDBC driver properties)</pre> | |
62 * <br> | |
63 * All DBCP properties are also supported by using the hibernate.dbcp prefix. | |
64 * A complete list can be found on the DBCP configuration page: | |
65 * <a href="http://jakarta.apache.org/commons/dbcp/configuration.html">http://jakarta.apache.org/commons/dbcp/configuration.html</a>. | |
66 * <br> | |
67 * <pre>Example: | |
68 * hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider | |
69 * hibernate.connection.driver_class org.hsqldb.jdbcDriver | |
70 * hibernate.connection.username sa | |
71 * hibernate.connection.password | |
72 * hibernate.connection.url jdbc:hsqldb:test | |
73 * hibernate.connection.pool_size 20 | |
74 * hibernate.dbcp.initialSize 10 | |
75 * hibernate.dbcp.maxWait 3000 | |
76 * hibernate.dbcp.validationQuery select 1 from dual</pre> | |
77 * | |
78 * <p>More information about configuring/using DBCP can be found on the | |
79 * <a href="http://jakarta.apache.org/commons/dbcp/">DBCP website</a>. | |
80 * There you will also find the DBCP wiki, mailing lists, issue tracking | |
81 * and other support facilities</p> | |
82 * | |
83 * @see org.hibernate.connection.ConnectionProvider | |
84 * @author Dirk Verbeeck | |
85 */ | |
86 public class DBCPConnectionProvider | |
87 implements ConnectionProvider | |
88 { | |
89 private static Logger log = Logger.getLogger(DBCPConnectionProvider.class); | |
90 | |
91 private static final String PREFIX = "hibernate.dbcp."; | |
92 | |
93 private BasicDataSource ds; | |
94 | |
95 // Old Environment property for backward-compatibility | |
96 // (property removed in Hibernate3) | |
97 private static final String DBCP_PS_MAXACTIVE = | |
98 "hibernate.dbcp.ps.maxActive"; | |
99 | |
100 // Property doesn't exists in Hibernate2 | |
101 private static final String AUTOCOMMIT = | |
102 "hibernate.connection.autocommit"; | |
103 | |
104 public void configure(Properties props) throws HibernateException { | |
105 try { | |
106 log.debug("Configure DBCPConnectionProvider"); | |
107 | |
108 // DBCP properties used to create the BasicDataSource | |
109 Properties dbcpProperties = new Properties(); | |
110 | |
111 // DriverClass & url | |
112 String jdbcDriverClass = props.getProperty(Environment.DRIVER); | |
113 String jdbcUrl = props.getProperty(Environment.URL); | |
114 dbcpProperties.put("driverClassName", jdbcDriverClass); | |
115 dbcpProperties.put("url", jdbcUrl); | |
116 | |
117 // Username / password | |
118 String username = props.getProperty(Environment.USER); | |
119 String password = props.getProperty(Environment.PASS); | |
120 dbcpProperties.put("username", username); | |
121 dbcpProperties.put("password", password); | |
122 | |
123 // Isolation level | |
124 String isolationLevel = props.getProperty(Environment.ISOLATION); | |
125 if (isolationLevel != null | |
126 && (isolationLevel = isolationLevel.trim()).length() > 0) { | |
127 dbcpProperties.put("defaultTransactionIsolation", isolationLevel); | |
128 } | |
129 | |
130 // Turn off autocommit (unless autocommit property is set) | |
131 String autocommit = props.getProperty(AUTOCOMMIT); | |
132 if (autocommit != null | |
133 && (autocommit = autocommit.trim()).length() > 0) { | |
134 dbcpProperties.put("defaultAutoCommit", autocommit); | |
135 } else { | |
136 dbcpProperties.put("defaultAutoCommit", String.valueOf(Boolean.FALSE)); | |
137 } | |
138 | |
139 // Pool size | |
140 String poolSize = props.getProperty(Environment.POOL_SIZE); | |
141 if (poolSize != null | |
142 && (poolSize = poolSize.trim()).length() > 0 | |
143 && Integer.parseInt(poolSize) > 0) { | |
144 dbcpProperties.put("maxActive", poolSize); | |
145 } | |
146 | |
147 // Copy all "driver" properties into "connectionProperties" | |
148 Properties driverProps = | |
149 ConnectionProviderFactory.getConnectionProperties(props); | |
150 | |
151 if (driverProps.size() > 0) { | |
152 StringBuilder connectionProperties = new StringBuilder(); | |
153 for (Iterator iter = driverProps.entrySet().iterator(); | |
154 iter.hasNext(); | |
155 ) { | |
156 Map.Entry entry = (Map.Entry)iter.next(); | |
157 String key = (String)entry.getKey(); | |
158 String value = (String)entry.getValue(); | |
159 connectionProperties | |
160 .append(key) | |
161 .append('=') | |
162 .append(value); | |
163 if (iter.hasNext()) { | |
164 connectionProperties.append(';'); | |
165 } | |
166 } | |
167 dbcpProperties.put( | |
168 "connectionProperties", connectionProperties.toString()); | |
169 } | |
170 | |
171 // Copy all DBCP properties removing the prefix | |
172 for (Iterator iter = props.entrySet().iterator() ; iter.hasNext() ;) { | |
173 Map.Entry entry = (Map.Entry)iter.next(); | |
174 String key = (String)entry.getKey(); | |
175 if (key.startsWith(PREFIX)) { | |
176 String property = key.substring(PREFIX.length()); | |
177 String value = (String)entry.getValue(); | |
178 dbcpProperties.put(property, value); | |
179 } | |
180 } | |
181 | |
182 // Backward-compatibility | |
183 if (props.getProperty(DBCP_PS_MAXACTIVE) != null) { | |
184 dbcpProperties.put( | |
185 "poolPreparedStatements", | |
186 String.valueOf(Boolean.TRUE)); | |
187 dbcpProperties.put( | |
188 "maxOpenPreparedStatements", | |
189 props.getProperty(DBCP_PS_MAXACTIVE)); | |
190 } | |
191 | |
192 // Some debug info | |
193 /* // commented out, because it leaks the password | |
194 if (log.isDebugEnabled()) { | |
195 log.debug("Creating a DBCP BasicDataSource" + | |
196 " with the following DBCP factory properties:"); | |
197 StringWriter sw = new StringWriter(); | |
198 dbcpProperties.list(new PrintWriter(sw, true)); | |
199 log.debug(sw.toString()); | |
200 } | |
201 */ | |
202 | |
203 // Let the factory create the pool | |
204 ds = (BasicDataSource)BasicDataSourceFactory | |
205 .createDataSource(dbcpProperties); | |
206 | |
207 // This needs to be done manually as it is somehow ignored | |
208 // by the BasicDataSourceFactory if you set it as a dbcpProperty | |
209 String connectionInitSqls = props.getProperty("connectionInitSqls"); | |
210 if (connectionInitSqls != null) { | |
211 StringTokenizer tokenizer = new StringTokenizer(connectionInitSqls, ";"); | |
212 ds.setConnectionInitSqls(Collections.list(tokenizer)); | |
213 } | |
214 // The BasicDataSource has lazy initialization | |
215 // borrowing a connection will start the DataSource | |
216 // and make sure it is configured correctly. | |
217 | |
218 // Connection conn = ds.getConnection(); | |
219 // conn.close(); | |
220 } | |
221 catch (Exception e) { | |
222 String message = "Could not create a DBCP pool"; | |
223 log.fatal(message, e); | |
224 if (ds != null) { | |
225 BasicDataSource x = ds; ds = null; | |
226 try { | |
227 x.close(); | |
228 } | |
229 catch (SQLException sqle) { | |
230 } | |
231 } | |
232 throw new HibernateException(message, e); | |
233 } | |
234 log.debug("Configure DBCPConnectionProvider complete"); | |
235 } | |
236 | |
237 public Connection getConnection() throws SQLException { | |
238 return ds.getConnection(); | |
239 } | |
240 | |
241 public void closeConnection(Connection conn) throws SQLException { | |
242 conn.close(); | |
243 } | |
244 | |
245 public void close() throws HibernateException { | |
246 try { | |
247 if (ds != null) { | |
248 BasicDataSource x = ds; ds = null; | |
249 x.close(); | |
250 } | |
251 } | |
252 catch (SQLException sqle) { | |
253 throw new HibernateException("Could not close DBCP pool", sqle); | |
254 } | |
255 } | |
256 | |
257 public boolean supportsAggressiveRelease() { | |
258 return false; | |
259 } | |
260 } | |
261 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |