Class ConnectionManagerImpl

  • All Implemented Interfaces:
    ConnectionManager

    public class ConnectionManagerImpl
    extends java.lang.Object
    implements ConnectionManager
    Manager of connections for a datastore, allowing caching of ManagedConnections, enlistment in transaction. Manages a "primary" and (optionally) a "secondary" ConnectionFactory. When caching is enabled it maintains caches of the allocated ManagedConnection per ExecutionContext (an EC can have a single ManagedConnection per ConnectionFactory at any time).

    The "allocateConnection" method can create connections and enlist them (like most normal persistence operations need) or create a connection and return it without enlisting it into a transaction, for example on a read-only operation, or when running non-transactional, or to get schema information.

    Connections can be locked per ExecutionContext basis. Locking of connections is used to handle the connection over to the user application. A locked connection denies any further access to the datastore, until the user application unlock it.

    • Field Detail

      • storeMgr

        final StoreManager storeMgr
        StoreManager that we are managing the connections for.
      • connectionCachingEnabled

        boolean connectionCachingEnabled
        Whether connection caching is enabled.
      • primaryConnectionFactory

        ConnectionFactory primaryConnectionFactory
        "Primary" ConnectionFactory, normally used for transactional operations.
      • secondaryConnectionFactory

        ConnectionFactory secondaryConnectionFactory
        "Secondary" ConnectionFactory, normally used for non-transactional operations.
      • primaryConnectionsCache

        java.util.Map<ExecutionContext,​ManagedConnection> primaryConnectionsCache
        Cache of ManagedConnection from the "primary" ConnectionFactory, keyed by the ExecutionContext (since an EC can have max 1 per factory).
      • secondaryConnectionsCache

        java.util.Map<ExecutionContext,​ManagedConnection> secondaryConnectionsCache
        Cache of ManagedConnection from the "secondary" ConnectionFactory, keyed by the ExecutionContext (since an EC can have max 1 per factory).
    • Constructor Detail

      • ConnectionManagerImpl

        public ConnectionManagerImpl​(StoreManager storeMgr)
        Constructor. This will register the "primary" and "secondary" ConnectionFactory objects.
        Parameters:
        storeMgr - Store manager for whom we are managing connections
    • Method Detail

      • close

        public void close()
        Description copied from interface: ConnectionManager
        Method to close the connection manager. This will close all open connections.
        Specified by:
        close in interface ConnectionManager
      • disableConnectionCaching

        public void disableConnectionCaching()
        Disable binding objects to ExecutionContext references, so automatically disables the connection caching.
        Specified by:
        disableConnectionCaching in interface ConnectionManager
      • getConnection

        public ManagedConnection getConnection​(ExecutionContext ec,
                                               java.util.Map options)
        Description copied from interface: ConnectionManager
        Accessor for a connection for the specified ExecutionContext. If there is an active transaction, a connection from the primary connection factory will be returned. If there is no active transaction, a connection from the secondary connection factory will be returned (unless the user has specified to just use the primary).
        Specified by:
        getConnection in interface ConnectionManager
        Parameters:
        ec - execution context
        options - connection options
        Returns:
        The ManagedConnection
      • getConnection

        public ManagedConnection getConnection​(int isolationLevel)
        Description copied from interface: ConnectionManager
        Accessor for a connection for the specified transaction isolation level. This is used for schema and sequence access operations.
        Specified by:
        getConnection in interface ConnectionManager
        Parameters:
        isolationLevel - Isolation level (-1 implies use the default for the datastore).
        Returns:
        The ManagedConnection
      • getConnection

        public ManagedConnection getConnection​(boolean primary,
                                               ExecutionContext ec,
                                               Transaction txn)
        Description copied from interface: ConnectionManager
        Accessor for a connection from the specified factory, for the specified ExecutionContext dependent on whether the connection will be enlisted.
        Specified by:
        getConnection in interface ConnectionManager
        Parameters:
        primary - Whether to take use the "primary" connection factory, otherwise takes the "secondary"
        ec - ExecutionContext
        txn - The Transaction
        Returns:
        The ManagedConnection
      • removeManagedConnection

        protected void removeManagedConnection​(boolean primary,
                                               ExecutionContext ec)
        Method to remove the ManagedConnection from the cache.
        Parameters:
        primary - Whether to use the primary ConnectionFactory
        ec - ExecutionContext
      • getManagedConnection

        protected ManagedConnection getManagedConnection​(boolean primary,
                                                         ExecutionContext ec)
        Get a ManagedConnection from the cache.
        Parameters:
        primary - Whether to use the primary ConnectionFactory
        ec - ExecutionContext
        Returns:
        The managed connection
      • putManagedConnection

        protected void putManagedConnection​(boolean primary,
                                            ExecutionContext ec,
                                            ManagedConnection mconn)
        Put a ManagedConnection into the cache.
        Parameters:
        primary - Whether to use the primary ConnectionFactory
        ec - ExecutionContext
        mconn - The ManagedConnection
      • allocateManagedConnection

        private ManagedConnection allocateManagedConnection​(boolean primary,
                                                            ExecutionContext ec,
                                                            Transaction transaction,
                                                            java.util.Map options)
        Method to return a ManagedConnection for this ExecutionContext. If a connection for the ExecutionContext exists in the cache will return it. If no connection exists will create a new one using the ConnectionFactory.
        Parameters:
        primary - Whether this is the primary connection pool
        ec - Key in the pool
        transaction - The transaction
        options - Options for the connection (e.g isolation). These will override those of the txn itself
        Returns:
        The ManagedConnection
      • configureTransactionEventListener

        private void configureTransactionEventListener​(Transaction transaction,
                                                       ManagedConnection mconn)
        Configure a TransactionEventListener that closes the managed connection when a transaction commits or rolls back
        Parameters:
        transaction - The transaction that we add a listener to
        mconn - Managed connection being used