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