-
Notifications
You must be signed in to change notification settings - Fork 239
/
BasicDataSource.java
1794 lines (1628 loc) · 62.9 KB
/
BasicDataSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.dbcp;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.logging.Logger;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import javax.sql.DataSource;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
/**
* <p>Basic implementation of <code>javax.sql.DataSource</code> that is
* configured via JavaBeans properties. This is not the only way to
* combine the <em>commons-dbcp</em> and <em>commons-pool</em> packages,
* but provides a "one stop shopping" solution for basic requirements.</p>
*
* <p>Users extending this class should take care to use appropriate accessors
* rather than accessing protected fields directly to ensure thread-safety.</p>
*
* @author Glenn L. Nielsen
* @author Craig R. McClanahan
* @author Dirk Verbeeck
* @version $Revision$ $Date$
*/
public class BasicDataSource implements DataSource {
static {
// Attempt to prevent deadlocks - see DBCP - 272
DriverManager.getDrivers();
}
// ------------------------------------------------------------- Properties
/**
* The default auto-commit state of connections created by this pool.
*/
protected volatile boolean defaultAutoCommit = true;
/**
* Returns the default auto-commit property.
*
* @return true if default auto-commit is enabled
*/
public boolean getDefaultAutoCommit() {
return this.defaultAutoCommit;
}
/**
* <p>Sets default auto-commit state of connections returned by this
* datasource.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param defaultAutoCommit default auto-commit value
*/
public void setDefaultAutoCommit(boolean defaultAutoCommit) {
this.defaultAutoCommit = defaultAutoCommit;
this.restartNeeded = true;
}
/**
* The default read-only state of connections created by this pool.
*/
protected transient Boolean defaultReadOnly = null;
/**
* Returns the default readOnly property.
*
* @return true if connections are readOnly by default
*/
public boolean getDefaultReadOnly() {
Boolean val = defaultReadOnly;
if (val != null) {
return val.booleanValue();
}
return false;
}
/**
* <p>Sets defaultReadonly property.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param defaultReadOnly default read-only value
*/
public void setDefaultReadOnly(boolean defaultReadOnly) {
this.defaultReadOnly = defaultReadOnly ? Boolean.TRUE : Boolean.FALSE;
this.restartNeeded = true;
}
/**
* The default TransactionIsolation state of connections created by this pool.
*/
protected volatile int defaultTransactionIsolation =
PoolableConnectionFactory.UNKNOWN_TRANSACTIONISOLATION;
/**
* Returns the default transaction isolation state of returned connections.
*
* @return the default value for transaction isolation state
* @see Connection#getTransactionIsolation
*/
public int getDefaultTransactionIsolation() {
return this.defaultTransactionIsolation;
}
/**
* <p>Sets the default transaction isolation state for returned
* connections.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param defaultTransactionIsolation the default transaction isolation
* state
* @see Connection#getTransactionIsolation
*/
public void setDefaultTransactionIsolation(int defaultTransactionIsolation) {
this.defaultTransactionIsolation = defaultTransactionIsolation;
this.restartNeeded = true;
}
/**
* The default "catalog" of connections created by this pool.
*/
protected volatile String defaultCatalog = null;
/**
* Returns the default catalog.
*
* @return the default catalog
*/
public String getDefaultCatalog() {
return this.defaultCatalog;
}
/**
* <p>Sets the default catalog.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param defaultCatalog the default catalog
*/
public void setDefaultCatalog(String defaultCatalog) {
if ((defaultCatalog != null) && (defaultCatalog.trim().length() > 0)) {
this.defaultCatalog = defaultCatalog;
}
else {
this.defaultCatalog = null;
}
this.restartNeeded = true;
}
/**
* The fully qualified Java class name of the JDBC driver to be used.
*/
protected String driverClassName = null;
/**
* Returns the JDBC driver class name.
*
* @return the JDBC driver class name
*/
public synchronized String getDriverClassName() {
return this.driverClassName;
}
/**
* <p>Sets the JDBC driver class name.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param driverClassName the class name of the JDBC driver
*/
public synchronized void setDriverClassName(String driverClassName) {
if ((driverClassName != null) && (driverClassName.trim().length() > 0)) {
this.driverClassName = driverClassName;
}
else {
this.driverClassName = null;
}
this.restartNeeded = true;
}
/**
* The class loader instance to use to load the JDBC driver. If not
* specified, {@link Class#forName(String)} is used to load the JDBC driver.
* If specified, {@link Class#forName(String, boolean, ClassLoader)} is
* used.
*/
protected ClassLoader driverClassLoader = null;
/**
* Returns the class loader specified for loading the JDBC driver. Returns
* <code>null</code> if no class loader has been explicitly specified.
*/
public synchronized ClassLoader getDriverClassLoader() {
return this.driverClassLoader;
}
/**
* <p>Sets the class loader to be used to load the JDBC driver.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param driverClassLoader the class loader with which to load the JDBC
* driver
*/
public synchronized void setDriverClassLoader(
ClassLoader driverClassLoader) {
this.driverClassLoader = driverClassLoader;
this.restartNeeded = true;
}
/**
* True means that borrowObject returns the most recently used ("last in")
* connection in the pool (if there are idle connections available). False
* means that the pool behaves as a FIFO queue - connections are taken from
* the idle instance pool in the order that they are returned to the pool.
*/
private boolean lifo = GenericObjectPool.DEFAULT_LIFO;
/**
* Returns the LIFO property.
*
* @return true if connection pool behaves as a LIFO queue.
*
* @see #lifo
*/
public synchronized boolean getLifo() {
return this.lifo;
}
/**
* Sets the LIFO property. True means the pool behaves as a LIFO queue;
* false means FIFO.
*
* @param lifo the new value for the LIFO property
*
*/
public synchronized void setLifo(boolean lifo) {
this.lifo = lifo;
if (connectionPool != null) {
connectionPool.setLifo(lifo);
}
}
/**
* The maximum number of active connections that can be allocated from
* this pool at the same time, or negative for no limit.
*/
protected int maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE;
/**
* <p>Returns the maximum number of active connections that can be
* allocated at the same time.
* </p>
* <p>A negative number means that there is no limit.</p>
*
* @return the maximum number of active connections
*/
public synchronized int getMaxActive() {
return this.maxActive;
}
/**
* Sets the maximum number of active connections that can be
* allocated at the same time. Use a negative value for no limit.
*
* @param maxActive the new value for maxActive
* @see #getMaxActive()
*/
public synchronized void setMaxActive(int maxActive) {
this.maxActive = maxActive;
if (connectionPool != null) {
connectionPool.setMaxActive(maxActive);
}
}
/**
* The maximum number of connections that can remain idle in the
* pool, without extra ones being destroyed, or negative for no limit.
* If maxIdle is set too low on heavily loaded systems it is possible you
* will see connections being closed and almost immediately new connections
* being opened. This is a result of the active threads momentarily closing
* connections faster than they are opening them, causing the number of idle
* connections to rise above maxIdle. The best value for maxIdle for heavily
* loaded system will vary but the default is a good starting point.
*/
protected int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
/**
* <p>Returns the maximum number of connections that can remain idle in the
* pool. Excess idle connections are destroyed on return to the pool.
* </p>
* <p>A negative value indicates that there is no limit</p>
*
* @return the maximum number of idle connections
*/
public synchronized int getMaxIdle() {
return this.maxIdle;
}
/**
* Sets the maximum number of connections that can remain idle in the
* pool. Excess idle connections are destroyed on return to the pool.
*
* @see #getMaxIdle()
* @param maxIdle the new value for maxIdle
*/
public synchronized void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
if (connectionPool != null) {
connectionPool.setMaxIdle(maxIdle);
}
}
/**
* The minimum number of active connections that can remain idle in the
* pool, without extra ones being created when the evictor runs, or 0 to create none.
* The pool attempts to ensure that minIdle connections are available when the idle object evictor
* runs. The value of this property has no effect unless {@link #timeBetweenEvictionRunsMillis}
* has a positive value.
*/
protected int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
/**
* Returns the minimum number of idle connections in the pool. The pool attempts
* to ensure that minIdle connections are available when the idle object evictor
* runs. The value of this property has no effect unless {@link #timeBetweenEvictionRunsMillis}
* has a positive value.
*
* @return the minimum number of idle connections
* @see GenericObjectPool#getMinIdle()
*/
public synchronized int getMinIdle() {
return this.minIdle;
}
/**
* Sets the minimum number of idle connections in the pool. The pool attempts
* to ensure that minIdle connections are available when the idle object evictor
* runs. The value of this property has no effect unless {@link #timeBetweenEvictionRunsMillis}
* has a positive value.
*
* @param minIdle the new value for minIdle
* @see GenericObjectPool#setMinIdle(int)
*/
public synchronized void setMinIdle(int minIdle) {
this.minIdle = minIdle;
if (connectionPool != null) {
connectionPool.setMinIdle(minIdle);
}
}
/**
* The initial number of connections that are created when the pool
* is started.
*
* @since 1.2
*/
protected int initialSize = 0;
/**
* Returns the initial size of the connection pool.
*
* @return the number of connections created when the pool is initialized
*/
public synchronized int getInitialSize() {
return this.initialSize;
}
/**
* <p>Sets the initial size of the connection pool.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param initialSize the number of connections created when the pool
* is initialized
*/
public synchronized void setInitialSize(int initialSize) {
this.initialSize = initialSize;
this.restartNeeded = true;
}
/**
* The maximum number of milliseconds that the pool will wait (when there
* are no available connections) for a connection to be returned before
* throwing an exception, or <= 0 to wait indefinitely.
*/
protected long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
/**
* <p>Returns the maximum number of milliseconds that the pool will wait
* for a connection to be returned before throwing an exception.
* </p>
* <p>A value less than or equal to zero means the pool is set to wait
* indefinitely.</p>
*
* @return the maxWait property value
*/
public synchronized long getMaxWait() {
return this.maxWait;
}
/**
* <p>Sets the maxWait property.
* </p>
* <p>Use -1 to make the pool wait indefinitely.
* </p>
*
* @param maxWait the new value for maxWait
* @see #getMaxWait()
*/
public synchronized void setMaxWait(long maxWait) {
this.maxWait = maxWait;
if (connectionPool != null) {
connectionPool.setMaxWait(maxWait);
}
}
/**
* Prepared statement pooling for this pool. When this property is set to <code>true</code>
* both PreparedStatements and CallableStatements are pooled.
*/
protected boolean poolPreparedStatements = false;
/**
* Returns true if we are pooling statements.
*
* @return true if prepared and callable statements are pooled
*/
public synchronized boolean isPoolPreparedStatements() {
return this.poolPreparedStatements;
}
/**
* <p>Sets whether to pool statements or not.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param poolingStatements pooling on or off
*/
public synchronized void setPoolPreparedStatements(boolean poolingStatements) {
this.poolPreparedStatements = poolingStatements;
this.restartNeeded = true;
}
/**
* <p>The maximum number of open statements that can be allocated from
* the statement pool at the same time, or non-positive for no limit. Since
* a connection usually only uses one or two statements at a time, this is
* mostly used to help detect resource leaks.</p>
*
* <p>Note: As of version 1.3, CallableStatements (those produced by {@link Connection#prepareCall})
* are pooled along with PreparedStatements (produced by {@link Connection#prepareStatement})
* and <code>maxOpenPreparedStatements</code> limits the total number of prepared or callable statements
* that may be in use at a given time.</p>
*/
protected int maxOpenPreparedStatements = GenericKeyedObjectPool.DEFAULT_MAX_TOTAL;
/**
* Gets the value of the {@link #maxOpenPreparedStatements} property.
*
* @return the maximum number of open statements
* @see #maxOpenPreparedStatements
*/
public synchronized int getMaxOpenPreparedStatements() {
return this.maxOpenPreparedStatements;
}
/**
* <p>Sets the value of the {@link #maxOpenPreparedStatements}
* property.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param maxOpenStatements the new maximum number of prepared statements
* @see #maxOpenPreparedStatements
*/
public synchronized void setMaxOpenPreparedStatements(int maxOpenStatements) {
this.maxOpenPreparedStatements = maxOpenStatements;
this.restartNeeded = true;
}
/**
* The indication of whether objects will be validated before being
* borrowed from the pool. If the object fails to validate, it will be
* dropped from the pool, and we will attempt to borrow another.
*/
protected boolean testOnBorrow = true;
/**
* Returns the {@link #testOnBorrow} property.
*
* @return true if objects are validated before being borrowed from the
* pool
*
* @see #testOnBorrow
*/
public synchronized boolean getTestOnBorrow() {
return this.testOnBorrow;
}
/**
* Sets the {@link #testOnBorrow} property. This property determines
* whether or not the pool will validate objects before they are borrowed
* from the pool. For a <code>true</code> value to have any effect, the
* <code>validationQuery</code> property must be set to a non-null string.
*
* @param testOnBorrow new value for testOnBorrow property
*/
public synchronized void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
if (connectionPool != null) {
connectionPool.setTestOnBorrow(testOnBorrow);
}
}
/**
* The indication of whether objects will be validated before being
* returned to the pool.
*/
protected boolean testOnReturn = false;
/**
* Returns the value of the {@link #testOnReturn} property.
*
* @return true if objects are validated before being returned to the
* pool
* @see #testOnReturn
*/
public synchronized boolean getTestOnReturn() {
return this.testOnReturn;
}
/**
* Sets the <code>testOnReturn</code> property. This property determines
* whether or not the pool will validate objects before they are returned
* to the pool. For a <code>true</code> value to have any effect, the
* <code>validationQuery</code> property must be set to a non-null string.
*
* @param testOnReturn new value for testOnReturn property
*/
public synchronized void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
if (connectionPool != null) {
connectionPool.setTestOnReturn(testOnReturn);
}
}
/**
* The number of milliseconds to sleep between runs of the idle object
* evictor thread. When non-positive, no idle object evictor thread will
* be run.
*/
protected long timeBetweenEvictionRunsMillis =
GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
/**
* Returns the value of the {@link #timeBetweenEvictionRunsMillis}
* property.
*
* @return the time (in milliseconds) between evictor runs
* @see #timeBetweenEvictionRunsMillis
*/
public synchronized long getTimeBetweenEvictionRunsMillis() {
return this.timeBetweenEvictionRunsMillis;
}
/**
* Sets the {@link #timeBetweenEvictionRunsMillis} property.
*
* @param timeBetweenEvictionRunsMillis the new time between evictor runs
* @see #timeBetweenEvictionRunsMillis
*/
public synchronized void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
if (connectionPool != null) {
connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
}
}
/**
* The number of objects to examine during each run of the idle object
* evictor thread (if any).
*/
protected int numTestsPerEvictionRun =
GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
/**
* Returns the value of the {@link #numTestsPerEvictionRun} property.
*
* @return the number of objects to examine during idle object evictor
* runs
* @see #numTestsPerEvictionRun
*/
public synchronized int getNumTestsPerEvictionRun() {
return this.numTestsPerEvictionRun;
}
/**
* Sets the value of the {@link #numTestsPerEvictionRun} property.
*
* @param numTestsPerEvictionRun the new {@link #numTestsPerEvictionRun}
* value
* @see #numTestsPerEvictionRun
*/
public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
if (connectionPool != null) {
connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
}
}
/**
* The minimum amount of time an object may sit idle in the pool before it
* is eligible for eviction by the idle object evictor (if any).
*/
protected long minEvictableIdleTimeMillis =
GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
/**
* Returns the {@link #minEvictableIdleTimeMillis} property.
*
* @return the value of the {@link #minEvictableIdleTimeMillis} property
* @see #minEvictableIdleTimeMillis
*/
public synchronized long getMinEvictableIdleTimeMillis() {
return this.minEvictableIdleTimeMillis;
}
/**
* Sets the {@link #minEvictableIdleTimeMillis} property.
*
* @param minEvictableIdleTimeMillis the minimum amount of time an object
* may sit idle in the pool
* @see #minEvictableIdleTimeMillis
*/
public synchronized void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
if (connectionPool != null) {
connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
}
}
/**
* The minimum amount of time a connection may sit idle in the pool before
* it is eligible for eviction by the idle object evictor, with the extra
* condition that at least "minIdle" connections remain in the pool.
* Note that {@code minEvictableIdleTimeMillis} takes precedence over this
* parameter. See {@link #getSoftMinEvictableIdleTimeMillis()}.
*/
private long softMinEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
/**
* Sets the minimum amount of time a connection may sit idle in the pool
* before it is eligible for eviction by the idle object evictor, with the
* extra condition that at least "minIdle" connections remain in the pool.
*
* @param softMinEvictableIdleTimeMillis minimum amount of time a
* connection may sit idle in the pool before it is eligible for eviction,
* assuming there are minIdle idle connections in the pool.
* @since 1.4.1
* @see #getSoftMinEvictableIdleTimeMillis
*/
public synchronized void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
if (connectionPool != null) {
connectionPool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
}
}
/**
* <p>Returns the minimum amount of time a connection may sit idle in the
* pool before it is eligible for eviction by the idle object evictor, with
* the extra condition that at least "minIdle" connections remain in the
* pool.</p>
*
* <p>When {@link #getMinEvictableIdleTimeMillis() miniEvictableIdleTimeMillis}
* is set to a positive value, miniEvictableIdleTimeMillis is examined
* first by the idle connection evictor - i.e. when idle connections are
* visited by the evictor, idle time is first compared against
* {@code minEvictableIdleTimeMillis} (without considering the number of idle
* connections in the pool) and then against
* {@code softMinEvictableIdleTimeMillis}, including the {@code minIdle},
* constraint.</p>
*
* @return minimum amount of time a connection may sit idle in the pool before
* it is eligible for eviction, assuming there are minIdle idle connections
* in the pool
* @since 1.4.1
*/
public synchronized long getSoftMinEvictableIdleTimeMillis() {
return softMinEvictableIdleTimeMillis;
}
/**
* The indication of whether objects will be validated by the idle object
* evictor (if any). If an object fails to validate, it will be dropped
* from the pool.
*/
protected boolean testWhileIdle = false;
/**
* Returns the value of the {@link #testWhileIdle} property.
*
* @return true if objects examined by the idle object evictor are
* validated
* @see #testWhileIdle
*/
public synchronized boolean getTestWhileIdle() {
return this.testWhileIdle;
}
/**
* Sets the <code>testWhileIdle</code> property. This property determines
* whether or not the idle object evictor will validate connections. For a
* <code>true</code> value to have any effect, the
* <code>validationQuery</code> property must be set to a non-null string.
*
* @param testWhileIdle new value for testWhileIdle property
*/
public synchronized void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
if (connectionPool != null) {
connectionPool.setTestWhileIdle(testWhileIdle);
}
}
/**
* [Read Only] The current number of active connections that have been
* allocated from this data source.
*
* @return the current number of active connections
*/
public synchronized int getNumActive() {
if (connectionPool != null) {
return connectionPool.getNumActive();
} else {
return 0;
}
}
/**
* [Read Only] The current number of idle connections that are waiting
* to be allocated from this data source.
*
* @return the current number of idle connections
*/
public synchronized int getNumIdle() {
if (connectionPool != null) {
return connectionPool.getNumIdle();
} else {
return 0;
}
}
/**
* The connection password to be passed to our JDBC driver to establish
* a connection.
*/
protected volatile String password = null;
/**
* Returns the password passed to the JDBC driver to establish connections.
*
* @return the connection password
*/
public String getPassword() {
return this.password;
}
/**
* <p>Sets the {@link #password}.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param password new value for the password
*/
public void setPassword(String password) {
this.password = password;
this.restartNeeded = true;
}
/**
* The connection URL to be passed to our JDBC driver to establish
* a connection.
*/
protected String url = null;
/**
* Returns the JDBC connection {@link #url} property.
*
* @return the {@link #url} passed to the JDBC driver to establish
* connections
*/
public synchronized String getUrl() {
return this.url;
}
/**
* <p>Sets the {@link #url}.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param url the new value for the JDBC connection url
*/
public synchronized void setUrl(String url) {
this.url = url;
this.restartNeeded = true;
}
/**
* The connection username to be passed to our JDBC driver to
* establish a connection.
*/
protected String username = null;
/**
* Returns the JDBC connection {@link #username} property.
*
* @return the {@link #username} passed to the JDBC driver to establish
* connections
*/
public String getUsername() {
return this.username;
}
/**
* <p>Sets the {@link #username}.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param username the new value for the JDBC connection username
*/
public void setUsername(String username) {
this.username = username;
this.restartNeeded = true;
}
/**
* The SQL query that will be used to validate connections from this pool
* before returning them to the caller. If specified, this query
* <strong>MUST</strong> be an SQL SELECT statement that returns at least
* one row.
*/
protected volatile String validationQuery = null;
/**
* Returns the validation query used to validate connections before
* returning them.
*
* @return the SQL validation query
* @see #validationQuery
*/
public String getValidationQuery() {
return this.validationQuery;
}
/**
* <p>Sets the {@link #validationQuery}.</p>
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param validationQuery the new value for the validation query
*/
public void setValidationQuery(String validationQuery) {
if ((validationQuery != null) && (validationQuery.trim().length() > 0)) {
this.validationQuery = validationQuery;
} else {
this.validationQuery = null;
}
this.restartNeeded = true;
}
/**
* Timeout in seconds before connection validation queries fail.
*
* @since 1.3
*/
protected volatile int validationQueryTimeout = -1;
/**
* Returns the validation query timeout.
*
* @return the timeout in seconds before connection validation queries fail.
* @since 1.3
*/
public int getValidationQueryTimeout() {
return validationQueryTimeout;
}
/**
* Sets the validation query timeout, the amount of time, in seconds, that
* connection validation will wait for a response from the database when
* executing a validation query. Use a value less than or equal to 0 for
* no timeout.
* <p>
* Note: this method currently has no effect once the pool has been
* initialized. The pool is initialized the first time one of the
* following methods is invoked: <code>getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter.</code></p>
*
* @param timeout new validation query timeout value in seconds
* @since 1.3
*/
public void setValidationQueryTimeout(int timeout) {
this.validationQueryTimeout = timeout;
restartNeeded = true;
}
/**
* These SQL statements run once after a Connection is created.
* <p>
* This property can be used for example to run ALTER SESSION SET
* NLS_SORT=XCYECH in an Oracle Database only once after connection
* creation.
* </p>
*
* @since 1.3
*/
protected volatile List connectionInitSqls;
/**
* Returns the list of SQL statements executed when a physical connection
* is first created. Returns an empty list if there are no initialization