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