Class PoolConnectionProvider

  • All Implemented Interfaces:
    IConnectionProvider

    public class PoolConnectionProvider
    extends java.lang.Object
    implements IConnectionProvider
    Provides a simple connection pool with some thread-local behavior so that requests for a connection within the same thread will get the same connection back. Cooperates with the TransactionFactory to provide a JEE-flavored experience, and makes it easy to write code which will work in both J2SE and JEE environments. Does not support distributed transactions.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) int allocated  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean checkConnectionFailure​(java.sql.SQLException x)
      Simple check to see if the exception is related to a connection error, in which case the connection shouldn't be returned to the pool when closed
      protected void clearActiveConnection()
      Remove the active connection on this thread.
      void close()
      The caller is telling us they no longer need to use the pool so we can free any internal resources.
      void commitTransaction()
      Commit the current transaction if there's an actual connection associated with this thread (i.e.
      protected void configureConnection​(java.sql.Connection c)
      Apply an configuration steps to a new connection
      void describe​(java.lang.String prefix, java.lang.StringBuilder cfg, java.lang.String key)
      Describe self, for writing configuration information to log file for test record purposes
      java.sql.Connection getConnection()
      Obtain a new connection to a data-source which this provider has been configured for.
      int getPoolSize()
      gets the pool size for the pooled connection
      IDatabaseTranslator getTranslator()
      Get the translator associated with this connection provider.
      protected void returnConnection​(PooledConnection pc, boolean reuse)
      Called when the connection is closed, which in this case is pretty much a NOP, because this connection will stay active on this thread until the transaction commits
      void rollbackTransaction()
      Roll back the transaction for the connection associated with this thread, if one exists
      void setCloseOnAnyError()
      Configure the pool to close connections after an error instead of returning them to the pool.
      void setNewConnectionHandler​(java.util.function.Consumer<java.sql.Connection> handler)
      Setter for the newConnectionHandler
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • allocated

        int allocated
    • Constructor Detail

      • PoolConnectionProvider

        public PoolConnectionProvider​(IConnectionProvider cp,
                                      int maxPoolSize)
        Public constructor
        Parameters:
        cp -
        maxPoolSize -
    • Method Detail

      • setCloseOnAnyError

        public void setCloseOnAnyError()
        Configure the pool to close connections after an error instead of returning them to the pool. Exceptions related to connection errors will always mark the connection as no longer usable.
      • setNewConnectionHandler

        public void setNewConnectionHandler​(java.util.function.Consumer<java.sql.Connection> handler)
        Setter for the newConnectionHandler
        Parameters:
        handler -
      • configureConnection

        protected void configureConnection​(java.sql.Connection c)
        Apply an configuration steps to a new connection
        Parameters:
        c -
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
        Description copied from interface: IConnectionProvider
        Obtain a new connection to a data-source which this provider has been configured for.
        Specified by:
        getConnection in interface IConnectionProvider
        Returns:
        Throws:
        java.sql.SQLException
      • returnConnection

        protected void returnConnection​(PooledConnection pc,
                                        boolean reuse)
        Called when the connection is closed, which in this case is pretty much a NOP, because this connection will stay active on this thread until the transaction commits
        Parameters:
        pc -
      • clearActiveConnection

        protected void clearActiveConnection()
                                      throws java.sql.SQLException
        Remove the active connection on this thread. Called when the transaction completes
        Throws:
        java.sql.SQLException
      • checkConnectionFailure

        protected boolean checkConnectionFailure​(java.sql.SQLException x)
        Simple check to see if the exception is related to a connection error, in which case the connection shouldn't be returned to the pool when closed
        Parameters:
        x -
        Returns:
      • getTranslator

        public IDatabaseTranslator getTranslator()
        Description copied from interface: IConnectionProvider
        Get the translator associated with this connection provider. Supports interpretation of SQLExceptions and the ability to tweak SQL statements to handle differences between databases
        Specified by:
        getTranslator in interface IConnectionProvider
        Returns:
      • commitTransaction

        public void commitTransaction()
                               throws java.sql.SQLException
        Description copied from interface: IConnectionProvider
        Commit the current transaction if there's an actual connection associated with this thread (i.e. if getConnection() has been called at least once), otherwise do nothing.
        Specified by:
        commitTransaction in interface IConnectionProvider
        Throws:
        java.sql.SQLException
      • rollbackTransaction

        public void rollbackTransaction()
                                 throws java.sql.SQLException
        Description copied from interface: IConnectionProvider
        Roll back the transaction for the connection associated with this thread, if one exists
        Specified by:
        rollbackTransaction in interface IConnectionProvider
        Throws:
        java.sql.SQLException
      • describe

        public void describe​(java.lang.String prefix,
                             java.lang.StringBuilder cfg,
                             java.lang.String key)
        Description copied from interface: IConnectionProvider
        Describe self, for writing configuration information to log file for test record purposes
        Specified by:
        describe in interface IConnectionProvider
      • close

        public void close()
        The caller is telling us they no longer need to use the pool so we can free any internal resources. This also let's us check for anything currently in-use that shouldn't be
        Throws:
        java.lang.IllegalStateException - if there are open connections or a transaction is active.