1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.store; |
21 |
|
|
22 |
|
import java.io.PrintWriter; |
23 |
|
import java.io.StringWriter; |
24 |
|
import java.sql.Connection; |
25 |
|
import java.sql.SQLException; |
26 |
|
import java.util.Iterator; |
27 |
|
import java.util.Properties; |
28 |
|
|
29 |
|
import org.apache.commons.dbcp2.BasicDataSource; |
30 |
|
import org.apache.commons.dbcp2.BasicDataSourceFactory; |
31 |
|
import org.hibernate.HibernateException; |
32 |
|
import org.hibernate.cfg.Environment; |
33 |
|
import org.hibernate.connection.ConnectionProvider; |
34 |
|
import org.hibernate.connection.ConnectionProviderFactory; |
35 |
|
import org.slf4j.Logger; |
36 |
|
import org.slf4j.LoggerFactory; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@see |
84 |
|
@author |
85 |
|
|
|
|
| 67.2% |
Uncovered Elements: 42 (128) |
Complexity: 31 |
Complexity Density: 0.34 |
|
86 |
|
public class DBCPConnectionProvider implements ConnectionProvider |
87 |
|
{ |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
private static final Logger SHUTDOWN_LOGGER = LoggerFactory.getLogger("org.xwiki.shutdown"); |
92 |
|
|
93 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(DBCPConnectionProvider.class); |
94 |
|
|
95 |
|
private static final String PREFIX = "hibernate.dbcp."; |
96 |
|
|
97 |
|
private BasicDataSource ds; |
98 |
|
|
99 |
|
|
100 |
|
private static final String COMPATIBILITY_PS_MAXACTIVE = "ps.maxActive"; |
101 |
|
|
102 |
|
|
103 |
|
private static final String COMPATIBILITY_MAXACTIVE = "maxActive"; |
104 |
|
private static final String COMPATIBILITY_MAXWAIT = "maxWait"; |
105 |
|
|
106 |
|
|
107 |
|
private static final String AUTOCOMMIT = "hibernate.connection.autocommit"; |
108 |
|
|
|
|
| 64.9% |
Uncovered Elements: 33 (94) |
Complexity: 22 |
Complexity Density: 0.31 |
|
109 |
60 |
@Override... |
110 |
|
public void configure(Properties props) throws HibernateException |
111 |
|
{ |
112 |
60 |
try { |
113 |
60 |
LOGGER.debug("Configure DBCPConnectionProvider"); |
114 |
|
|
115 |
|
|
116 |
60 |
Properties dbcpProperties = new Properties(); |
117 |
|
|
118 |
|
|
119 |
60 |
String jdbcDriverClass = props.getProperty(Environment.DRIVER); |
120 |
60 |
dbcpProperties.put("driverClassName", jdbcDriverClass); |
121 |
|
|
122 |
60 |
String jdbcUrl = System.getProperty(Environment.URL); |
123 |
60 |
if (jdbcUrl == null) { |
124 |
34 |
jdbcUrl = props.getProperty(Environment.URL); |
125 |
|
} |
126 |
60 |
dbcpProperties.put("url", jdbcUrl); |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
60 |
String username = props.getProperty(Environment.USER); |
132 |
60 |
if (username != null) { |
133 |
60 |
dbcpProperties.put("username", username); |
134 |
|
} |
135 |
60 |
String password = props.getProperty(Environment.PASS); |
136 |
60 |
if (password != null) { |
137 |
60 |
dbcpProperties.put("password", password); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
60 |
String isolationLevel = props.getProperty(Environment.ISOLATION); |
142 |
60 |
if ((isolationLevel != null) && (isolationLevel.trim().length() > 0)) { |
143 |
0 |
dbcpProperties.put("defaultTransactionIsolation", isolationLevel); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
60 |
String autocommit = props.getProperty(AUTOCOMMIT); |
149 |
60 |
if ((autocommit != null) && (autocommit.trim().length() > 0)) { |
150 |
0 |
dbcpProperties.put("defaultAutoCommit", autocommit); |
151 |
|
} else { |
152 |
60 |
dbcpProperties.put("defaultAutoCommit", String.valueOf(Boolean.FALSE)); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
60 |
String poolSize = props.getProperty(Environment.POOL_SIZE); |
157 |
60 |
if ((poolSize != null) && (poolSize.trim().length() > 0) && (Integer.parseInt(poolSize) > 0)) { |
158 |
0 |
dbcpProperties.put("maxTotal", poolSize); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
60 |
Properties driverProps = ConnectionProviderFactory.getConnectionProperties(props); |
163 |
60 |
if (driverProps.size() > 0) { |
164 |
60 |
StringBuilder connectionProperties = new StringBuilder(); |
165 |
360 |
for (Iterator iter = driverProps.keySet().iterator(); iter.hasNext();) { |
166 |
300 |
String key = (String) iter.next(); |
167 |
300 |
String value = driverProps.getProperty(key); |
168 |
300 |
connectionProperties.append(key).append('=').append(value); |
169 |
300 |
if (iter.hasNext()) { |
170 |
240 |
connectionProperties.append(';'); |
171 |
|
} |
172 |
|
} |
173 |
60 |
dbcpProperties.put("connectionProperties", connectionProperties.toString()); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
60 |
for (Object element : props.keySet()) { |
178 |
5672 |
String key = String.valueOf(element); |
179 |
5672 |
if (key.startsWith(PREFIX)) { |
180 |
240 |
String property = key.substring(PREFIX.length()); |
181 |
240 |
String value = props.getProperty(key); |
182 |
|
|
183 |
|
|
184 |
240 |
switch (property) { |
185 |
0 |
case COMPATIBILITY_PS_MAXACTIVE: |
186 |
0 |
dbcpProperties.put("poolPreparedStatements", String.valueOf(Boolean.TRUE)); |
187 |
0 |
dbcpProperties.put("maxOpenPreparedStatements", value); |
188 |
0 |
break; |
189 |
0 |
case COMPATIBILITY_MAXACTIVE: |
190 |
0 |
dbcpProperties.put("maxTotal", value); |
191 |
0 |
break; |
192 |
0 |
case COMPATIBILITY_MAXWAIT: |
193 |
0 |
dbcpProperties.put("maxWaitMillis", value); |
194 |
0 |
break; |
195 |
240 |
default: |
196 |
240 |
dbcpProperties.put(property, value); |
197 |
|
} |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
60 |
if (LOGGER.isDebugEnabled()) { |
203 |
0 |
LOGGER.debug("Creating a DBCP BasicDataSource with the following DBCP factory properties:"); |
204 |
0 |
StringWriter sw = new StringWriter(); |
205 |
0 |
dbcpProperties.list(new PrintWriter(sw, true)); |
206 |
0 |
LOGGER.debug(sw.toString()); |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
60 |
this.ds = BasicDataSourceFactory.createDataSource(dbcpProperties); |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
60 |
Connection conn = this.ds.getConnection(); |
216 |
60 |
conn.close(); |
217 |
|
|
218 |
|
|
219 |
60 |
logStatistics(); |
220 |
|
} catch (Exception e) { |
221 |
0 |
String message = "Could not create a DBCP pool. There is an error in the Hibernate configuration file, " |
222 |
|
+ "please review it."; |
223 |
0 |
LOGGER.error(message, e); |
224 |
0 |
if (this.ds != null) { |
225 |
0 |
try { |
226 |
0 |
this.ds.close(); |
227 |
|
} catch (Exception e2) { |
228 |
|
|
229 |
|
} |
230 |
0 |
this.ds = null; |
231 |
|
} |
232 |
0 |
throw new HibernateException(message, e); |
233 |
|
} |
234 |
60 |
LOGGER.debug("Configure DBCPConnectionProvider complete"); |
235 |
|
} |
236 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
237 |
167927 |
@Override... |
238 |
|
public Connection getConnection() throws SQLException |
239 |
|
{ |
240 |
|
|
241 |
|
|
242 |
167917 |
if (this.ds == null) { |
243 |
0 |
throw new SQLException("Database Connection Pool has not been started or is already stopped!"); |
244 |
|
} |
245 |
|
|
246 |
167910 |
Connection conn = null; |
247 |
167921 |
try { |
248 |
167926 |
conn = this.ds.getConnection(); |
249 |
|
} finally { |
250 |
167948 |
logStatistics(); |
251 |
|
} |
252 |
167931 |
return conn; |
253 |
|
} |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
255 |
167940 |
@Override... |
256 |
|
public void closeConnection(Connection conn) throws SQLException |
257 |
|
{ |
258 |
167944 |
try { |
259 |
167936 |
conn.close(); |
260 |
|
} finally { |
261 |
167955 |
logStatistics(); |
262 |
|
} |
263 |
|
} |
264 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
265 |
60 |
@Override... |
266 |
|
public void close() throws HibernateException |
267 |
|
{ |
268 |
60 |
SHUTDOWN_LOGGER.debug("Stopping Database Connection Pool..."); |
269 |
60 |
logStatistics(); |
270 |
60 |
try { |
271 |
60 |
if (this.ds != null) { |
272 |
60 |
this.ds.close(); |
273 |
60 |
this.ds = null; |
274 |
|
} else { |
275 |
0 |
LOGGER.warn("Cannot close Database Connection Pool (not initialized)"); |
276 |
|
} |
277 |
|
} catch (Exception e) { |
278 |
0 |
throw new HibernateException("Could not close DBCP pool", e); |
279 |
|
} |
280 |
60 |
SHUTDOWN_LOGGER.debug("Database Connection Pool has been stopped"); |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
@link |
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
294 |
0 |
@Override... |
295 |
|
public boolean supportsAggressiveRelease() |
296 |
|
{ |
297 |
0 |
return false; |
298 |
|
} |
299 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
300 |
336018 |
protected void logStatistics()... |
301 |
|
{ |
302 |
336009 |
if (LOGGER.isDebugEnabled()) { |
303 |
0 |
LOGGER.debug("active: [{}] (max: [{}]), idle: [{}] (max: [{}])", this.ds.getNumActive(), |
304 |
|
this.ds.getMaxTotal(), this.ds.getNumIdle(), this.ds.getMaxIdle()); |
305 |
|
} |
306 |
|
} |
307 |
|
} |