Package org.datanucleus.store.connection
Class AbstractManagedConnection
- java.lang.Object
-
- org.datanucleus.store.connection.AbstractManagedConnection
-
- All Implemented Interfaces:
ManagedConnection
public abstract class AbstractManagedConnection extends Object implements ManagedConnection
Abstract implementation of a managed connection. There are three primary modes for a connection.- Transactional - the commit of connection is controlled external to this class, and when we release the connection it is handed back into a pooled state, available for reuse.
- Non-transactional (1) - the commit of the connection happens at close and when we release the connection it closes the connection (after committing it).
- Non-transactional (2) - the commit of the connection happens at release, and when we release the connection it is handed back into a pooled state, available for reuse.
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
closeOnRelease
Whether we should close() when release() of the connection is called.protected boolean
commitOnRelease
Whether we should commit() the connection on release().protected Object
conn
The underlying (datastore-specific) connection.protected List<ManagedConnectionResourceListener>
listeners
Listeners for the connection.protected boolean
locked
Whether the connection is locked for use.protected int
useCount
Count on the number of outstanding uses of this connection.
-
Constructor Summary
Constructors Constructor Description AbstractManagedConnection()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(ManagedConnectionResourceListener listener)
Registers a ManagedConnectionResourceListener to be notified of events.void
close()
Close the connection to the datastore.boolean
closeAfterTransactionEnd()
Convenience method for whether this connection should be closed after the end of transaction.boolean
closeOnRelease()
boolean
commitOnRelease()
XAResource
getXAResource()
Obtain an XAResource which can be enlisted in a transaction Override this if you intend on supporting this as an XA resource (default = not supported).protected void
incrementUseCount()
boolean
isLocked()
void
lock()
void
release()
Release this connection back to us so we can pool it if required.void
releaseSavepoint(String name)
Release the named savepoint (or do nothing if not supported).void
removeListener(ManagedConnectionResourceListener listener)
Deregister a ManagedConnectionResourceListener.void
rollbackToSavepoint(String name)
Rollback the connection to the named savepoint (or do nothing if not supported).void
setCloseOnRelease(boolean close)
void
setCommitOnRelease(boolean commit)
void
setSavepoint(String name)
Set this position in the txn as a savepoint with the provided name (if supported, otherwise do nothing).String
toString()
Method to return a string form of this object for convenience debug.void
transactionFlushed()
Flush the connection.void
transactionPreClose()
Prepare the connection for end of transaction.void
unlock()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.datanucleus.store.connection.ManagedConnection
getConnection
-
-
-
-
Field Detail
-
conn
protected Object conn
The underlying (datastore-specific) connection.
-
closeOnRelease
protected boolean closeOnRelease
Whether we should close() when release() of the connection is called.
-
commitOnRelease
protected boolean commitOnRelease
Whether we should commit() the connection on release().
-
locked
protected boolean locked
Whether the connection is locked for use.
-
listeners
protected List<ManagedConnectionResourceListener> listeners
Listeners for the connection.
-
useCount
protected int useCount
Count on the number of outstanding uses of this connection. Incremented on get. Decremented on release().
-
-
Method Detail
-
incrementUseCount
protected void incrementUseCount()
-
close
public void close()
Description copied from interface:ManagedConnection
Close the connection to the datastore. It most invoke the operationsManagedConnectionResourceListener.managedConnectionPreClose()
andManagedConnectionResourceListener.managedConnectionPostClose()
. The listeners are unregistered after this method is invoked.- Specified by:
close
in interfaceManagedConnection
-
release
public void release()
Release this connection back to us so we can pool it if required. In the case of a transactional connection it is allocated and released and always pooled (not committed) during the transaction. With non-transactional connections, they can be pooled (where selected), or not (default).- Specified by:
release
in interfaceManagedConnection
-
transactionFlushed
public void transactionFlushed()
Description copied from interface:ManagedConnection
Flush the connection. It must invoke the operationManagedConnectionResourceListener.transactionFlushed()
- Specified by:
transactionFlushed
in interfaceManagedConnection
-
transactionPreClose
public void transactionPreClose()
Description copied from interface:ManagedConnection
Prepare the connection for end of transaction. It must invoke the operationManagedConnectionResourceListener.transactionPreClose()
- Specified by:
transactionPreClose
in interfaceManagedConnection
-
setCloseOnRelease
public void setCloseOnRelease(boolean close)
- Specified by:
setCloseOnRelease
in interfaceManagedConnection
-
setCommitOnRelease
public void setCommitOnRelease(boolean commit)
- Specified by:
setCommitOnRelease
in interfaceManagedConnection
-
closeOnRelease
public boolean closeOnRelease()
- Specified by:
closeOnRelease
in interfaceManagedConnection
-
commitOnRelease
public boolean commitOnRelease()
- Specified by:
commitOnRelease
in interfaceManagedConnection
-
addListener
public void addListener(ManagedConnectionResourceListener listener)
Description copied from interface:ManagedConnection
Registers a ManagedConnectionResourceListener to be notified of events.- Specified by:
addListener
in interfaceManagedConnection
- Parameters:
listener
- The listener
-
removeListener
public void removeListener(ManagedConnectionResourceListener listener)
Description copied from interface:ManagedConnection
Deregister a ManagedConnectionResourceListener.- Specified by:
removeListener
in interfaceManagedConnection
- Parameters:
listener
- The listener
-
isLocked
public boolean isLocked()
- Specified by:
isLocked
in interfaceManagedConnection
-
lock
public void lock()
- Specified by:
lock
in interfaceManagedConnection
-
unlock
public void unlock()
- Specified by:
unlock
in interfaceManagedConnection
-
getXAResource
public XAResource getXAResource()
Obtain an XAResource which can be enlisted in a transaction Override this if you intend on supporting this as an XA resource (default = not supported).- Specified by:
getXAResource
in interfaceManagedConnection
- Returns:
- The XA resource
-
closeAfterTransactionEnd
public boolean closeAfterTransactionEnd()
Description copied from interface:ManagedConnection
Convenience method for whether this connection should be closed after the end of transaction. In DN 2.x, 3.0, 3.1 this was always true, and a connection lasted until txn commit, and then had to get a new connection. In DN 3.2+ this is configurable per datastore connection factory.- Specified by:
closeAfterTransactionEnd
in interfaceManagedConnection
- Returns:
- Whether the ConnectionManager should call close() on it when a txn ends
-
setSavepoint
public void setSavepoint(String name)
Description copied from interface:ManagedConnection
Set this position in the txn as a savepoint with the provided name (if supported, otherwise do nothing).- Specified by:
setSavepoint
in interfaceManagedConnection
- Parameters:
name
- Name of savepoint
-
releaseSavepoint
public void releaseSavepoint(String name)
Description copied from interface:ManagedConnection
Release the named savepoint (or do nothing if not supported).- Specified by:
releaseSavepoint
in interfaceManagedConnection
- Parameters:
name
- Name of savepoint
-
rollbackToSavepoint
public void rollbackToSavepoint(String name)
Description copied from interface:ManagedConnection
Rollback the connection to the named savepoint (or do nothing if not supported).- Specified by:
rollbackToSavepoint
in interfaceManagedConnection
- Parameters:
name
- Name of savepoint
-
-