Package org.datanucleus.store.connection
Class ConnectionManagerImpl
- java.lang.Object
-
- org.datanucleus.store.connection.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 Summary
Fields Modifier and Type Field Description (package private) boolean
connectionCachingEnabled
Whether connection caching is enabled.(package private) PersistenceNucleusContext
nucleusContext
(package private) ConnectionFactory
primaryConnectionFactory
"Primary" ConnectionFactory, normally used for transactional operations.(package private) 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).(package private) ConnectionFactory
secondaryConnectionFactory
"Secondary" ConnectionFactory, normally used for non-transactional operations.(package private) 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).(package private) StoreManager
storeMgr
StoreManager that we are managing the connections for.
-
Constructor Summary
Constructors Constructor Description ConnectionManagerImpl(StoreManager storeMgr)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private ManagedConnection
allocateManagedConnection(boolean primary, ExecutionContext ec, Transaction transaction, java.util.Map options)
Method to return a ManagedConnection for this ExecutionContext.void
close()
Method to close the connection manager.void
closeAllConnections(ExecutionContext ec)
Method to close all pooled connections for the specified ExecutionContext.private void
configureTransactionEventListener(Transaction transaction, ManagedConnection mconn)
Configure a TransactionEventListener that closes the managed connection when a transaction commits or rolls backvoid
disableConnectionCaching()
Disable binding objects to ExecutionContext references, so automatically disables the connection caching.ManagedConnection
getConnection(boolean primary, ExecutionContext ec, Transaction txn)
Accessor for a connection from the specified factory, for the specified ExecutionContext dependent on whether the connection will be enlisted.ManagedConnection
getConnection(int isolationLevel)
Accessor for a connection for the specified transaction isolation level.ManagedConnection
getConnection(ExecutionContext ec, java.util.Map options)
Accessor for a connection for the specified ExecutionContext.protected ManagedConnection
getManagedConnection(boolean primary, ExecutionContext ec)
Get a ManagedConnection from the cache.protected void
putManagedConnection(boolean primary, ExecutionContext ec, ManagedConnection mconn)
Put a ManagedConnection into the cache.protected void
removeManagedConnection(boolean primary, ExecutionContext ec)
Method to remove the ManagedConnection from the cache.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.datanucleus.store.connection.ConnectionManager
getConnection
-
-
-
-
Field Detail
-
storeMgr
final StoreManager storeMgr
StoreManager that we are managing the connections for.
-
nucleusContext
final PersistenceNucleusContext nucleusContext
-
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 interfaceConnectionManager
-
disableConnectionCaching
public void disableConnectionCaching()
Disable binding objects to ExecutionContext references, so automatically disables the connection caching.- Specified by:
disableConnectionCaching
in interfaceConnectionManager
-
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 interfaceConnectionManager
- Parameters:
ec
- execution contextoptions
- 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 interfaceConnectionManager
- 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 interfaceConnectionManager
- Parameters:
primary
- Whether to take use the "primary" connection factory, otherwise takes the "secondary"ec
- ExecutionContexttxn
- The Transaction- Returns:
- The ManagedConnection
-
closeAllConnections
public void closeAllConnections(ExecutionContext ec)
Description copied from interface:ConnectionManager
Method to close all pooled connections for the specified ExecutionContext.- Specified by:
closeAllConnections
in interfaceConnectionManager
- Parameters:
ec
- The ExecutionContext
-
removeManagedConnection
protected void removeManagedConnection(boolean primary, ExecutionContext ec)
Method to remove the ManagedConnection from the cache.- Parameters:
primary
- Whether to use the primary ConnectionFactoryec
- ExecutionContext
-
getManagedConnection
protected ManagedConnection getManagedConnection(boolean primary, ExecutionContext ec)
Get a ManagedConnection from the cache.- Parameters:
primary
- Whether to use the primary ConnectionFactoryec
- 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 ConnectionFactoryec
- ExecutionContextmconn
- 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 poolec
- Key in the pooltransaction
- The transactionoptions
- 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 tomconn
- Managed connection being used
-
-