2121 * <li>Traces connections that have been leaked</li>
2222 * </ul>
2323 */
24- public final class ConnectionPool implements DataSourcePool {
24+ final class ConnectionPool implements DataSourcePool {
2525
2626 private final ReentrantLock heartbeatLock = new ReentrantLock (false );
2727 private final ReentrantLock notifyLock = new ReentrantLock (false );
@@ -55,7 +55,7 @@ public final class ConnectionPool implements DataSourcePool {
5555 * A value of 0 means no limit (no trimming based on max age).
5656 */
5757 private final long maxAgeMillis ;
58- private boolean captureStackTrace ;
58+ private final boolean captureStackTrace ;
5959 private final int maxStackTraceSize ;
6060 private long lastTrimTime ;
6161 /**
@@ -68,21 +68,21 @@ public final class ConnectionPool implements DataSourcePool {
6868 private int maxConnections ;
6969 private int warningSize ;
7070 private final int waitTimeoutMillis ;
71- private int pstmtCacheSize ;
71+ private final int pstmtCacheSize ;
7272 private final PooledConnectionQueue queue ;
7373 private Timer heartBeatTimer ;
7474 /**
7575 * Used to find and close() leaked connections. Leaked connections are
7676 * thought to be busy but have not been used for some time. Each time a
7777 * connection is used it sets it's lastUsedTime.
7878 */
79- private long leakTimeMinutes ;
79+ private final long leakTimeMinutes ;
8080 private final LongAdder pscHit = new LongAdder ();
8181 private final LongAdder pscMiss = new LongAdder ();
8282 private final LongAdder pscPut = new LongAdder ();
8383 private final LongAdder pscRem = new LongAdder ();
8484
85- public ConnectionPool (String name , DataSourceConfig params ) {
85+ ConnectionPool (String name , DataSourceConfig params ) {
8686 this .config = params ;
8787 this .name = name ;
8888 this .notify = params .getAlert ();
@@ -236,13 +236,13 @@ private void initialiseConnections() throws SQLException {
236236 * That is, if we think the username doesn't exist in the DB, initialise the DB using the owner credentials.
237237 */
238238 private void initialiseDatabase () throws SQLException {
239- try (Connection connection = createUnpooledConnection (connectionProps , false )) {
239+ try (Connection connection = createConnection (connectionProps , false )) {
240240 // successfully obtained a connection so skip initDatabase
241241 connection .clearWarnings ();
242242 } catch (SQLException e ) {
243243 Log .info ("Obtaining connection using ownerUsername:{0} to initialise database" , config .getOwnerUsername ());
244244 // expected when user does not exist, obtain a connection using owner credentials
245- try (Connection ownerConnection = createUnpooledConnection (config .getOwnerUsername (), config .getOwnerPassword ())) {
245+ try (Connection ownerConnection = createConnection (config .getOwnerUsername (), config .getOwnerPassword ())) {
246246 // initialise the DB (typically create the user/role using the owner credentials etc)
247247 InitDatabase initDatabase = config .getInitDatabase ();
248248 initDatabase .run (ownerConnection , config );
@@ -285,24 +285,18 @@ public int size() {
285285 /**
286286 * Increment the current pool size.
287287 */
288- public void inc () {
288+ void inc () {
289289 size .incrementAndGet ();
290290 }
291291
292292 /**
293293 * Decrement the current pool size.
294294 */
295- public void dec () {
295+ void dec () {
296296 size .decrementAndGet ();
297297 }
298298
299- /**
300- * Return the max size of stack traces used when trying to find connection pool leaks.
301- * <p>
302- * This is only used when {@link #isCaptureStackTrace()} is true.
303- * </p>
304- */
305- int getMaxStackTraceSize () {
299+ int maxStackTraceSize () {
306300 return maxStackTraceSize ;
307301 }
308302
@@ -444,18 +438,18 @@ private void initConnection(Connection conn) throws SQLException {
444438 /**
445439 * Create an un-pooled connection with the given username and password.
446440 */
447- public Connection createUnpooledConnection (String username , String password ) throws SQLException {
441+ private Connection createConnection (String username , String password ) throws SQLException {
448442 Properties properties = new Properties (connectionProps );
449443 properties .setProperty ("user" , username );
450444 properties .setProperty ("password" , password );
451- return createUnpooledConnection (properties , true );
445+ return createConnection (properties , true );
452446 }
453447
454- public Connection createUnpooledConnection () throws SQLException {
455- return createUnpooledConnection (connectionProps , true );
448+ private Connection createConnection () throws SQLException {
449+ return createConnection (connectionProps , true );
456450 }
457451
458- private Connection createUnpooledConnection (Properties properties , boolean notifyIsDown ) throws SQLException {
452+ private Connection createConnection (Properties properties , boolean notifyIsDown ) throws SQLException {
459453 try {
460454 Connection conn = DriverManager .getConnection (url , properties );
461455 initConnection (conn );
@@ -468,11 +462,6 @@ private Connection createUnpooledConnection(Properties properties, boolean notif
468462 }
469463 }
470464
471- /**
472- * Set a new maximum size. The pool should respect this new maximum
473- * immediately and not require a restart. You may want to increase the
474- * maxConnections if the pool gets large and hits the warning level.
475- */
476465 @ Override
477466 public void setMaxSize (int max ) {
478467 queue .setMaxSize (max );
@@ -501,22 +490,12 @@ public int getMinSize() {
501490 return minConnections ;
502491 }
503492
504- /**
505- * Set a new maximum size. The pool should respect this new maximum
506- * immediately and not require a restart. You may want to increase the
507- * maxConnections if the pool gets large and hits the warning and or alert
508- * levels.
509- */
510493 @ Override
511494 public void setWarningSize (int warningSize ) {
512495 queue .setWarningSize (warningSize );
513496 this .warningSize = warningSize ;
514497 }
515498
516- /**
517- * Return the warning size. When the pool hits this size it can send a
518- * notify message to an administrator.
519- */
520499 @ Override
521500 public int getWarningSize () {
522501 return warningSize ;
@@ -527,22 +506,15 @@ public int getWarningSize() {
527506 * the max size. These threads wait for connections to be returned by the
528507 * busy connections.
529508 */
530- public int getWaitTimeoutMillis () {
509+ int waitTimeoutMillis () {
531510 return waitTimeoutMillis ;
532511 }
533512
534- /**
535- * Return the time after which inactive connections are trimmed.
536- */
537- public int getMaxInactiveMillis () {
538- return maxInactiveMillis ;
539- }
540-
541513 /**
542514 * Return the maximum age a connection is allowed to be before it is trimmed
543515 * out of the pool. This value can be 0 which means there is no maximum age.
544516 */
545- public long getMaxAgeMillis () {
517+ long maxAgeMillis () {
546518 return maxAgeMillis ;
547519 }
548520
@@ -634,44 +606,13 @@ void returnConnectionReset(PooledConnection pooledConnection) {
634606 reset ();
635607 }
636608
637- /**
638- * Returns information describing connections that are currently being used.
639- */
640- public String getBusyConnectionInformation () {
641- return queue .getBusyConnectionInformation ();
642- }
643-
644- /**
645- * Dumps the busy connection information to the logs.
646- * <p>
647- * This includes the stackTrace elements if they are being captured. This is
648- * useful when needing to look a potential connection pool leaks.
649- */
650- public void dumpBusyConnectionInformation () {
651- queue .dumpBusyConnectionInformation ();
652- }
653-
654- /**
655- * Close any busy connections that have not been used for some time.
656- * <p>
657- * These connections are considered to have leaked from the connection pool.
658- * <p>
659- * Connection leaks occur when code doesn't ensure that connections are
660- * closed() after they have been finished with. There should be an
661- * appropriate try catch finally block to ensure connections are always
662- * closed and put back into the pool.
663- */
664- public void closeBusyConnections (long leakTimeMinutes ) {
665- queue .closeBusyConnections (leakTimeMinutes );
666- }
667-
668609 /**
669610 * Grow the pool by creating a new connection. The connection can either be
670611 * added to the available list, or returned.
671612 */
672613 PooledConnection createConnectionForQueue (int connId ) throws SQLException {
673614 try {
674- Connection c = createUnpooledConnection ();
615+ Connection c = createConnection ();
675616 PooledConnection pc = new PooledConnection (this , connId , c );
676617 pc .resetForUse ();
677618 notifyDataSourceIsUp ();
@@ -692,7 +633,7 @@ PooledConnection createConnectionForQueue(int connId) throws SQLException {
692633 * <li>Busy connections are closed when they are returned to the pool.</li>
693634 * </ul>
694635 */
695- public void reset () {
636+ private void reset () {
696637 queue .reset (leakTimeMinutes );
697638 inWarningMode .set (false );
698639 }
@@ -712,7 +653,7 @@ public Connection getConnection() throws SQLException {
712653 * will go into a wait if the pool has hit its maximum size.
713654 */
714655 private PooledConnection getPooledConnection () throws SQLException {
715- PooledConnection c = queue .getPooledConnection ();
656+ PooledConnection c = queue .obtainConnection ();
716657 if (captureStackTrace ) {
717658 c .setStackTrace (Thread .currentThread ().getStackTrace ());
718659 }
@@ -722,16 +663,6 @@ private PooledConnection getPooledConnection() throws SQLException {
722663 return c ;
723664 }
724665
725- /**
726- * Send a message to the DataSourceAlertListener to test it. This is so that
727- * you can make sure the alerter is configured correctly etc.
728- */
729- public void testAlert () {
730- if (notify != null ) {
731- notify .dataSourceWarning (this , "Just testing if alert message is sent successfully." );
732- }
733- }
734-
735666 /**
736667 * This will close all the free connections, and then go into a wait loop,
737668 * waiting for the busy connections to be freed.
@@ -810,30 +741,20 @@ public boolean isAutoCommit() {
810741 return autoCommit ;
811742 }
812743
813- /**
814- * Return the default transaction isolation level for the pool.
815- */
816- int getTransactionIsolation () {
744+ int transactionIsolation () {
817745 return transactionIsolation ;
818746 }
819747
820- /**
821- * Return true if the connection pool is currently capturing the StackTrace
822- * when connections are 'got' from the pool.
823- * <p>
824- * This is set to true to help diagnose connection pool leaks.
825- */
826- public boolean isCaptureStackTrace () {
748+ boolean captureStackTrace () {
827749 return captureStackTrace ;
828750 }
829751
830- /**
831- * Set this to true means that the StackElements are captured every time a
832- * connection is retrieved from the pool. This can be used to identify
833- * connection pool leaks.
834- */
835- public void setCaptureStackTrace (boolean captureStackTrace ) {
836- this .captureStackTrace = captureStackTrace ;
752+ long leakTimeMinutes () {
753+ return leakTimeMinutes ;
754+ }
755+
756+ int pstmtCacheSize () {
757+ return pstmtCacheSize ;
837758 }
838759
839760 /**
@@ -884,42 +805,6 @@ public void setLogWriter(PrintWriter writer) throws SQLException {
884805 throw new SQLException ("Method not supported" );
885806 }
886807
887- /**
888- * For detecting and closing leaked connections. Connections that have been
889- * busy for more than leakTimeMinutes are considered leaks and will be
890- * closed on a reset().
891- * <p>
892- * If you want to use a connection for that longer then you should consider
893- * creating an unpooled connection or setting longRunning to true on that
894- * connection.
895- * </p>
896- */
897- public void setLeakTimeMinutes (long leakTimeMinutes ) {
898- this .leakTimeMinutes = leakTimeMinutes ;
899- }
900-
901- /**
902- * Return the number of minutes after which a busy connection could be
903- * considered leaked from the connection pool.
904- */
905- public long getLeakTimeMinutes () {
906- return leakTimeMinutes ;
907- }
908-
909- /**
910- * Return the preparedStatement cache size.
911- */
912- public int getPstmtCacheSize () {
913- return pstmtCacheSize ;
914- }
915-
916- /**
917- * Set the preparedStatement cache size.
918- */
919- public void setPstmtCacheSize (int pstmtCacheSize ) {
920- this .pstmtCacheSize = pstmtCacheSize ;
921- }
922-
923808 /**
924809 * Return the current status of the connection pool.
925810 * <p>
@@ -929,7 +814,7 @@ public void setPstmtCacheSize(int pstmtCacheSize) {
929814 */
930815 @ Override
931816 public PoolStatus status (boolean reset ) {
932- return queue .getStatus (reset );
817+ return queue .status (reset );
933818 }
934819
935820 static final class Status implements PoolStatus {
0 commit comments