1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbcp2;
18
19 import java.sql.CallableStatement;
20 import java.sql.Connection;
21 import java.sql.PreparedStatement;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.NoSuchElementException;
25 import java.util.Objects;
26
27 import org.apache.commons.pool2.KeyedObjectPool;
28 import org.apache.commons.pool2.KeyedPooledObjectFactory;
29 import org.apache.commons.pool2.PooledObject;
30 import org.apache.commons.pool2.impl.DefaultPooledObject;
31 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class PoolingConnection extends DelegatingConnection<Connection>
46 implements KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement> {
47
48
49
50
51
52
53
54
55
56
57
58
59 public enum StatementType {
60
61
62
63
64
65
66 CALLABLE_STATEMENT,
67
68
69
70
71
72
73 PREPARED_STATEMENT
74 }
75
76
77 private KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pStmtPool;
78
79 private boolean clearStatementPoolOnReturn;
80
81
82
83
84
85
86
87 public PoolingConnection(final Connection connection) {
88 super(connection);
89 }
90
91
92
93
94
95
96
97
98
99 @Override
100 public void activateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject)
101 throws SQLException {
102 pooledObject.getObject().activate();
103 }
104
105
106
107
108
109 @Override
110 public synchronized void close() throws SQLException {
111 try {
112 if (null != pStmtPool) {
113 final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> oldPool = pStmtPool;
114 pStmtPool = null;
115 try {
116 oldPool.close();
117 } catch (final RuntimeException e) {
118 throw e;
119 } catch (final Exception e) {
120 throw new SQLException("Cannot close connection", e);
121 }
122 }
123 } finally {
124 try {
125 getDelegateInternal().close();
126 } finally {
127 setClosedInternal(true);
128 }
129 }
130 }
131
132
133
134
135
136
137
138
139 public void connectionReturnedToPool() throws SQLException {
140 if (pStmtPool != null && clearStatementPoolOnReturn) {
141 try {
142 pStmtPool.clear();
143 } catch (final Exception e) {
144 throw new SQLException("Error clearing statement pool", e);
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156
157 protected PStmtKey createKey(final String sql) {
158 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull());
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 protected PStmtKey createKey(final String sql, final int autoGeneratedKeys) {
173 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), autoGeneratedKeys);
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188 protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency) {
189 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), resultSetType, resultSetConcurrency);
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
207 final int resultSetHoldability) {
208 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), resultSetType, resultSetConcurrency,
209 resultSetHoldability);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
229 final int resultSetHoldability, final StatementType statementType) {
230 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), resultSetType, resultSetConcurrency,
231 resultSetHoldability, statementType);
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency,
249 final StatementType statementType) {
250 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), resultSetType, resultSetConcurrency, statementType);
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264 protected PStmtKey createKey(final String sql, final int[] columnIndexes) {
265 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), columnIndexes);
266 }
267
268
269
270
271
272
273
274
275
276
277
278 protected PStmtKey createKey(final String sql, final StatementType statementType) {
279 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), statementType, null);
280 }
281
282
283
284
285
286
287
288
289
290
291
292 protected PStmtKey createKey(final String sql, final String[] columnNames) {
293 return new PStmtKey(normalizeSQL(sql), getCatalogOrNull(), getSchemaOrNull(), columnNames);
294 }
295
296
297
298
299
300
301
302
303
304
305 @Override
306 public void destroyObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) throws SQLException {
307 if (pooledObject != null) {
308 @SuppressWarnings("resource")
309 final DelegatingPreparedStatement object = pooledObject.getObject();
310 if (object != null) {
311 @SuppressWarnings("resource")
312 final Statement innermostDelegate = object.getInnermostDelegate();
313 if (innermostDelegate != null) {
314 innermostDelegate.close();
315 }
316 }
317 }
318 }
319
320 private String getCatalogOrNull() {
321 try {
322 return getCatalog();
323 } catch (final SQLException ignored) {
324 return null;
325 }
326 }
327
328 private String getSchemaOrNull() {
329 try {
330 return getSchema();
331 } catch (final SQLException ignored) {
332 return null;
333 }
334 }
335
336
337
338
339
340
341
342 public KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> getStatementPool() {
343 return pStmtPool;
344 }
345
346
347
348
349
350
351
352
353
354
355 @SuppressWarnings("resource")
356 @Override
357 public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) throws SQLException {
358 if (null == key) {
359 throw new IllegalArgumentException("Prepared statement key is null or invalid.");
360 }
361 if (key.getStmtType() == StatementType.PREPARED_STATEMENT) {
362 final PreparedStatement statement = (PreparedStatement) key.createStatement(getDelegate());
363 @SuppressWarnings({"rawtypes", "unchecked" })
364 final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pStmtPool, this);
365 return new DefaultPooledObject<>(pps);
366 }
367 final CallableStatement statement = (CallableStatement) key.createStatement(getDelegate());
368 final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pStmtPool, this);
369 return new DefaultPooledObject<>(pcs);
370 }
371
372
373
374
375
376
377
378
379 protected String normalizeSQL(final String sql) {
380 return sql.trim();
381 }
382
383
384
385
386
387
388
389
390
391
392 @Override
393 public void passivateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject)
394 throws SQLException {
395 @SuppressWarnings("resource")
396 final DelegatingPreparedStatement dps = pooledObject.getObject();
397 dps.clearParameters();
398 dps.passivate();
399 }
400
401
402
403
404
405
406
407
408
409
410 private CallableStatement prepareCall(final PStmtKey key) throws SQLException {
411 return (CallableStatement) prepareStatement(key);
412 }
413
414
415
416
417
418
419
420
421
422
423 @Override
424 public CallableStatement prepareCall(final String sql) throws SQLException {
425 return prepareCall(createKey(sql, StatementType.CALLABLE_STATEMENT));
426 }
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441 @Override
442 public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency)
443 throws SQLException {
444 return prepareCall(createKey(sql, resultSetType, resultSetConcurrency, StatementType.CALLABLE_STATEMENT));
445 }
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 @Override
463 public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency,
464 final int resultSetHoldability) throws SQLException {
465 return prepareCall(createKey(sql, resultSetType, resultSetConcurrency,
466 resultSetHoldability, StatementType.CALLABLE_STATEMENT));
467 }
468
469
470
471
472
473
474
475
476
477
478 private PreparedStatement prepareStatement(final PStmtKey key) throws SQLException {
479 if (null == pStmtPool) {
480 throw new SQLException("Statement pool is null - closed or invalid PoolingConnection.");
481 }
482 try {
483 return pStmtPool.borrowObject(key);
484 } catch (final NoSuchElementException e) {
485 throw new SQLException("MaxOpenPreparedStatements limit reached", e);
486 } catch (final RuntimeException e) {
487 throw e;
488 } catch (final Exception e) {
489 throw new SQLException("Borrow prepareStatement from pool failed", e);
490 }
491 }
492
493
494
495
496
497
498
499
500
501
502 @Override
503 public PreparedStatement prepareStatement(final String sql) throws SQLException {
504 return prepareStatement(createKey(sql));
505 }
506
507
508
509
510
511
512
513
514
515
516
517
518
519 @Override
520 public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
521 return prepareStatement(createKey(sql, autoGeneratedKeys));
522 }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537 @Override
538 public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency)
539 throws SQLException {
540 return prepareStatement(createKey(sql, resultSetType, resultSetConcurrency));
541 }
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 @Override
559 public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency,
560 final int resultSetHoldability) throws SQLException {
561 return prepareStatement(createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability));
562 }
563
564
565
566
567
568
569
570
571
572
573
574
575
576 @Override
577 public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
578 return prepareStatement(createKey(sql, columnIndexes));
579 }
580
581
582
583
584
585
586
587
588
589
590
591
592 @Override
593 public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
594 return prepareStatement(createKey(sql, columnNames));
595 }
596
597
598
599
600
601
602
603
604 public void setClearStatementPoolOnReturn(final boolean clearStatementPoolOnReturn) {
605 this.clearStatementPoolOnReturn = clearStatementPoolOnReturn;
606 }
607
608
609
610
611
612
613
614 public void setStatementPool(final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pool) {
615 pStmtPool = pool;
616 }
617
618 @Override
619 public synchronized String toString() {
620 if (pStmtPool instanceof GenericKeyedObjectPool) {
621
622 final GenericKeyedObjectPool<?, ?> gkop = (GenericKeyedObjectPool<?, ?>) pStmtPool;
623 if (gkop.getFactory() == this) {
624 return "PoolingConnection: " + pStmtPool.getClass() + "@" + System.identityHashCode(pStmtPool);
625 }
626 }
627 return "PoolingConnection: " + Objects.toString(pStmtPool);
628 }
629
630
631
632
633
634
635
636
637
638
639 @Override
640 public boolean validateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) {
641 return true;
642 }
643 }