Class IuCommonDataSource

java.lang.Object
edu.iu.jdbc.pool.IuCommonDataSource
All Implemented Interfaces:
AutoCloseable, EventListener, CommonDataSource, ConnectionEventListener

public abstract class IuCommonDataSource extends Object implements CommonDataSource, ConnectionEventListener, AutoCloseable
Abstract common database connection pool implementation.

May be overridden to implement DataSource or XADataSource and integrate with an application runtime environment.

See Also:
  • Constructor Details

  • Method Details

    • getPooledConnection

      public IuPooledConnection getPooledConnection() throws SQLException
      Checks out a PooledConnection. UML Communication Diagram

      Implementation Note: The upstream DataSource implementation should discard this instance once the logical Connection view has been obtained. Application code will invoke Connection.close() to return the connection to the pool to be reused or retired. Note that invoking PooledConnection.close() will close the physical connection and remove it from the pool. This facilitates ejecting physical connections by an upstream pool manager.

      Returns:
      PooledConnection
      Throws:
      SQLException - if the connection fails due to a database error
    • connectionClosed

      public void connectionClosed(ConnectionEvent event)
      Specified by:
      connectionClosed in interface ConnectionEventListener
    • connectionErrorOccurred

      public void connectionErrorOccurred(ConnectionEvent event)
      Specified by:
      connectionErrorOccurred in interface ConnectionEventListener
    • getParentLogger

      public Logger getParentLogger()
      Specified by:
      getParentLogger in interface CommonDataSource
    • getLogWriter

      public PrintWriter getLogWriter() throws SQLException
      Specified by:
      getLogWriter in interface CommonDataSource
      Throws:
      SQLException
    • setLogWriter

      public void setLogWriter(PrintWriter out) throws SQLException
      Specified by:
      setLogWriter in interface CommonDataSource
      Throws:
      SQLException
    • setLoginTimeout

      public void setLoginTimeout(int seconds) throws SQLException
      Specified by:
      setLoginTimeout in interface CommonDataSource
      Throws:
      SQLException
    • getLoginTimeout

      public int getLoginTimeout()
      Specified by:
      getLoginTimeout in interface CommonDataSource
    • isClosed

      public boolean isClosed()
      Determines whether or not this database pool is closed.
      Returns:
      true if closed; else false
    • getAvailable

      public int getAvailable()
      Gets the number of open connections immediately available for reuse.
      Returns:
      number of open connections immediately available for reuse
    • getOpen

      public int getOpen()
      Gets a count of all open connections in the pool.
      Returns:
      count of all open connections
    • getUrl

      public String getUrl()
      Gets the URL used to initialize the downstream connection factory.
      Returns:
      Full JDBC URL
    • setUrl

      public void setUrl(String url)
      Sets the URL used to initialize the downstream connection factory.
      Parameters:
      url - Full JDBC URL
    • getUsername

      public String getUsername()
      Gets the database username used to initialize the downstream connection factory.
      Returns:
      Database username
    • setUsername

      public void setUsername(String username)
      Sets the database username used to initialize the downstream connection factory.
      Parameters:
      username - Database username
    • getSchema

      public String getSchema()
      Gets the database schema used to initialize the downstream connection factory.
      Returns:
      Database schema
    • setSchema

      public void setSchema(String schema)
      Sets the database schema used to initialize the downstream connection factory.
      Parameters:
      schema - Database schema
    • getMaxSize

      public int getMaxSize()
      Gets the maximum number of connections to allow in the pool.
      Returns:
      Pool max size
    • setMaxSize

      public void setMaxSize(int maxSize)
      Sets the maximum number of connections to allow in the pool.
      Parameters:
      maxSize - Pool max size
    • getMaxRetry

      public int getMaxRetry()
      Gets the maximum number of times a connection attempt will be retried before resulting in failure.
      Returns:
      maximum number of times a connection attempt will be retried before resulting in failure.
    • setMaxRetry

      public void setMaxRetry(int maxRetry)
      Gets the maximum number of times a connection attempt will be retried before resulting in failure.
      Parameters:
      maxRetry - maximum number of times a connection attempt will be retried before resulting in failure.
    • getMaxConnectionReuseCount

      public long getMaxConnectionReuseCount()
      Gets the maximum number of times a single connection can be used before ejecting from the pool.
      Returns:
      Per-connection max reuse count
    • setMaxConnectionReuseCount

      public void setMaxConnectionReuseCount(long maxConnectionReuseCount)
      Sets the maximum number of times a single connection can be used before ejecting from the pool.
      Parameters:
      maxConnectionReuseCount - Per-connection max reuse count
    • getMaxConnectionReuseTime

      public Duration getMaxConnectionReuseTime()
      Gets the maximum length of time a single connection can remain open before ejecting from the pool.
      Returns:
      Per-connection max reuse time
    • setMaxConnectionReuseTime

      public void setMaxConnectionReuseTime(Duration maxConnectionReuseTime)
      Gets the maximum length of time a single connection can remain open before ejecting from the pool.
      Parameters:
      maxConnectionReuseTime - Per-connection max reuse time
    • getAbandonedConnectionTimeout

      public Duration getAbandonedConnectionTimeout()
      Gets the maximum length of time a connection can be checked out from the pool before attempting to forcibly close and consider it abandoned.
      Returns:
      Abandoned connection timeout interval
    • setAbandonedConnectionTimeout

      public void setAbandonedConnectionTimeout(Duration abandonedConnectionTimeout)
      Sets the maximum length of time a connection can be checked out from the pool before attempting to forcibly close and consider it abandoned.
      Parameters:
      abandonedConnectionTimeout - Abandoned connection timeout interval
    • getShutdownTimeout

      public Duration getShutdownTimeout()
      Gets the maximum length of time to wait for all connections to close on shutdown.
      Returns:
      Maximum length of time to wait for all connections to close gracefully
    • setShutdownTimeout

      protected void setShutdownTimeout(Duration shutdownTimeout)
      Sets the maximum length of time to wait for all connections to close on shutdown.
      Parameters:
      shutdownTimeout - Maximum length of time to wait for all connections to close gracefully
    • getValidationQuery

      public String getValidationQuery()
      Gets the query to use for validating connections on creation, and intermittently before checking out from the pool.
      Returns:
      SQL select statement, must return a single row with a single non-null column; may be null to skip query validation
    • setValidationQuery

      public void setValidationQuery(String validationQuery)
      Sets the query to use for validating connections on creation, and intermittently before checking out from the pool.
      Parameters:
      validationQuery - SQL select statement, must return a single row with a single non-null column; may be null to skip query validation
    • getValidationInterval

      public Duration getValidationInterval()
      Gets the frequency at which to validate connections, when getValidationQuery() returns a non-null value.
      Returns:
      Frequency at which to validate connections; may be
    • setValidationInterval

      public void setValidationInterval(Duration validationInterval)
      Sets the frequency at which to validate connections, when getValidationQuery() returns a non-null value.
      Parameters:
      validationInterval - Frequency at which to validate connections; may be
    • setConnectionInitializer

      public void setConnectionInitializer(UnsafeFunction<Connection,Connection> connectionInitializer)
      Sets an optional transform function to be apply directly before checking out a connection from the pool.
      Parameters:
      connectionInitializer - UnsafeFunction: accepts and returns a Connection such that Wrapper.unwrap(Class) invoked on the return value delegates to the Connection passed as an argument; should not throw checked exceptions other than SQLException; may throw TimeoutException or InterruptedException.
    • setOnClose

      public void setOnClose(UnsafeRunnable onClose)
      Sets an optional shutdown hook to be invoked from close() after all physical connections managed by the pool have been closed.
      Parameters:
      onClose - UnsafeRunnable
    • close

      public void close() throws SQLException
      Waits for completion and closes all open connections.
      Specified by:
      close in interface AutoCloseable
      Throws:
      SQLException
    • toString

      public String toString()
      Overrides:
      toString in class Object