comparison flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java @ 168:86a1bd9cc50e

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

http://dive4elements.wald.intevation.org