Class ConnectionPool

java.lang.Object
io.ebean.datasource.pool.ConnectionPool
All Implemented Interfaces:
DataSourcePool, Wrapper, CommonDataSource, DataSource

public final class ConnectionPool extends Object implements DataSourcePool
A robust DataSource implementation.
  • Manages the number of connections closing connections that have been idle for some time.
  • Notifies when the datasource goes down and comes back up.
  • Provides PreparedStatement caching
  • Knows the busy connections
  • Traces connections that have been leaked
  • Constructor Details

  • Method Details

    • getParentLogger

      public Logger getParentLogger() throws SQLFeatureNotSupportedException
      Specified by:
      getParentLogger in interface CommonDataSource
      Throws:
      SQLFeatureNotSupportedException
    • isWrapperFor

      public boolean isWrapperFor(Class<?> arg0)
      Returns false.
      Specified by:
      isWrapperFor in interface Wrapper
    • unwrap

      public <T> T unwrap(Class<T> arg0) throws SQLException
      Not Implemented.
      Specified by:
      unwrap in interface Wrapper
      Throws:
      SQLException
    • getName

      public String getName()
      Return the dataSource name.
      Specified by:
      getName in interface DataSourcePool
    • size

      public int size()
      Specified by:
      size in interface DataSourcePool
    • inc

      public void inc()
      Increment the current pool size.
    • dec

      public void dec()
      Decrement the current pool size.
    • isDataSourceUp

      public boolean isDataSourceUp()
      Returns false when the dataSource is down.
      Specified by:
      isDataSourceUp in interface DataSourcePool
    • getDataSourceDownReason

      public SQLException getDataSourceDownReason()
      Specified by:
      getDataSourceDownReason in interface DataSourcePool
    • createUnpooledConnection

      public Connection createUnpooledConnection(String username, String password) throws SQLException
      Create an un-pooled connection with the given username and password.
      Throws:
      SQLException
    • createUnpooledConnection

      public Connection createUnpooledConnection() throws SQLException
      Throws:
      SQLException
    • setMaxSize

      public void setMaxSize(int max)
      Set a new maximum size. The pool should respect this new maximum immediately and not require a restart. You may want to increase the maxConnections if the pool gets large and hits the warning level.
      Specified by:
      setMaxSize in interface DataSourcePool
    • getMaxSize

      public int getMaxSize()
      Return the max size this pool can grow to.
    • setMinSize

      public void setMinSize(int min)
      Set the min size this pool should maintain.
    • getMinSize

      public int getMinSize()
      Return the min size this pool should maintain.
    • setWarningSize

      public void setWarningSize(int warningSize)
      Set a new maximum size. The pool should respect this new maximum immediately and not require a restart. You may want to increase the maxConnections if the pool gets large and hits the warning and or alert levels.
      Specified by:
      setWarningSize in interface DataSourcePool
    • getWarningSize

      public int getWarningSize()
      Return the warning size. When the pool hits this size it can send a notify message to an administrator.
      Specified by:
      getWarningSize in interface DataSourcePool
    • getWaitTimeoutMillis

      public int getWaitTimeoutMillis()
      Return the time in millis that threads will wait when the pool has hit the max size. These threads wait for connections to be returned by the busy connections.
    • getMaxInactiveMillis

      public int getMaxInactiveMillis()
      Return the time after which inactive connections are trimmed.
    • getMaxAgeMillis

      public long getMaxAgeMillis()
      Return the maximum age a connection is allowed to be before it is trimmed out of the pool. This value can be 0 which means there is no maximum age.
    • getBusyConnectionInformation

      public String getBusyConnectionInformation()
      Returns information describing connections that are currently being used.
    • dumpBusyConnectionInformation

      public void dumpBusyConnectionInformation()
      Dumps the busy connection information to the logs.

      This includes the stackTrace elements if they are being captured. This is useful when needing to look a potential connection pool leaks.

    • closeBusyConnections

      public void closeBusyConnections(long leakTimeMinutes)
      Close any busy connections that have not been used for some time.

      These connections are considered to have leaked from the connection pool.

      Connection leaks occur when code doesn't ensure that connections are closed() after they have been finished with. There should be an appropriate try catch finally block to ensure connections are always closed and put back into the pool.

    • reset

      public void reset()
      Close all the connections in the pool.
      • Checks that the database is up.
      • Resets the Alert level.
      • Closes busy connections that have not been used for some time (aka leaks).
      • This closes all the currently available connections.
      • Busy connections are closed when they are returned to the pool.
    • getConnection

      public Connection getConnection() throws SQLException
      Return a pooled connection.
      Specified by:
      getConnection in interface DataSource
      Throws:
      SQLException
    • testAlert

      public void testAlert()
      Send a message to the DataSourceAlertListener to test it. This is so that you can make sure the alerter is configured correctly etc.
    • shutdown

      public void shutdown()
      This will close all the free connections, and then go into a wait loop, waiting for the busy connections to be freed.

      The DataSources's should be shutdown AFTER thread pools. Leaked Connections are not waited on, as that would hang the server.

      Specified by:
      shutdown in interface DataSourcePool
    • offline

      public void offline()
      Specified by:
      offline in interface DataSourcePool
    • online

      public void online() throws SQLException
      Specified by:
      online in interface DataSourcePool
      Throws:
      SQLException
    • isOnline

      public boolean isOnline()
      Specified by:
      isOnline in interface DataSourcePool
    • isAutoCommit

      public boolean isAutoCommit()
      Return the default autoCommit setting for the pool.
      Specified by:
      isAutoCommit in interface DataSourcePool
    • isCaptureStackTrace

      public boolean isCaptureStackTrace()
      Return true if the connection pool is currently capturing the StackTrace when connections are 'got' from the pool.

      This is set to true to help diagnose connection pool leaks.

    • setCaptureStackTrace

      public void setCaptureStackTrace(boolean captureStackTrace)
      Set this to true means that the StackElements are captured every time a connection is retrieved from the pool. This can be used to identify connection pool leaks.
    • getConnection

      public Connection getConnection(String username, String password) throws SQLException
      Create an un-pooled connection with the given username and password.

      This uses the default isolation level and autocommit mode.

      Specified by:
      getConnection in interface DataSource
      Throws:
      SQLException
    • getLoginTimeout

      public int getLoginTimeout() throws SQLException
      Not implemented and shouldn't be used.
      Specified by:
      getLoginTimeout in interface CommonDataSource
      Specified by:
      getLoginTimeout in interface DataSource
      Throws:
      SQLException
    • setLoginTimeout

      public void setLoginTimeout(int seconds) throws SQLException
      Not implemented and shouldn't be used.
      Specified by:
      setLoginTimeout in interface CommonDataSource
      Specified by:
      setLoginTimeout in interface DataSource
      Throws:
      SQLException
    • getLogWriter

      public PrintWriter getLogWriter()
      Returns null.
      Specified by:
      getLogWriter in interface CommonDataSource
      Specified by:
      getLogWriter in interface DataSource
    • setLogWriter

      public void setLogWriter(PrintWriter writer) throws SQLException
      Not implemented.
      Specified by:
      setLogWriter in interface CommonDataSource
      Specified by:
      setLogWriter in interface DataSource
      Throws:
      SQLException
    • setLeakTimeMinutes

      public void setLeakTimeMinutes(long leakTimeMinutes)
      For detecting and closing leaked connections. Connections that have been busy for more than leakTimeMinutes are considered leaks and will be closed on a reset().

      If you want to use a connection for that longer then you should consider creating an unpooled connection or setting longRunning to true on that connection.

    • getLeakTimeMinutes

      public long getLeakTimeMinutes()
      Return the number of minutes after which a busy connection could be considered leaked from the connection pool.
    • getPstmtCacheSize

      public int getPstmtCacheSize()
      Return the preparedStatement cache size.
    • setPstmtCacheSize

      public void setPstmtCacheSize(int pstmtCacheSize)
      Set the preparedStatement cache size.
    • getStatus

      public PoolStatus getStatus(boolean reset)
      Return the current status of the connection pool.

      If you pass reset = true then the counters such as hitCount, waitCount and highWaterMark are reset.

      Specified by:
      getStatus in interface DataSourcePool