comparison flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java @ 2877:f0a67bc0e777 2.7

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

http://dive4elements.wald.intevation.org