Encapsulates JDBC Connection management logic needed by Hibernate.
The lifecycle is intended to span a logical series of interactions with the
database. Internally, this means the the lifecycle of the Session.
| Method from org.hibernate.jdbc.ConnectionManager Detail: |
public void afterStatement() {
if ( isAggressiveRelease() ) {
if ( isFlushing ) {
log.debug( "skipping aggressive-release due to flush cycle" );
}
else if ( batcher.hasOpenResources() ) {
log.debug( "skipping aggresive-release due to open resources on batcher" );
}
else if ( borrowedConnection != null ) {
log.debug( "skipping aggresive-release due to borrowed connection" );
}
else {
aggressiveRelease();
}
}
}
To be called after execution of each JDBC statement. Used to
conditionally release the JDBC connection aggressively if
the configured release mode indicates. |
public void afterTransaction() {
if ( isAfterTransactionRelease() ) {
aggressiveRelease();
}
else if ( isAggressiveReleaseNoTransactionCheck() && batcher.hasOpenResources() ) {
log.info( "forcing batcher resource cleanup on transaction completion; forgot to close ScrollableResults/Iterator?" );
batcher.closeStatements();
aggressiveRelease();
}
else if ( isOnCloseRelease() ) {
// log a message about potential connection leaks
log.debug( "transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!" );
}
batcher.unsetTransactionTimeout();
}
To be called after local transaction completion. Used to conditionally
release the JDBC connection aggressively if the configured release mode
indicates. |
public Connection borrowConnection() {
if ( isClosed ) {
throw new HibernateException( "connection manager has been closed" );
}
if ( isSuppliedConnection() ) {
return connection;
}
else {
if ( borrowedConnection == null ) {
borrowedConnection = BorrowedConnectionProxy.generateProxy( this );
}
return borrowedConnection;
}
}
|
public Connection close() {
try {
return cleanup();
}
finally {
isClosed = true;
}
}
To be called after Session completion. Used to release the JDBC
connection. |
public static ConnectionManager deserialize(ObjectInputStream ois,
SessionFactoryImplementor factory,
Interceptor interceptor,
ConnectionReleaseMode connectionReleaseMode,
JDBCContext jdbcContext) throws IOException {
return new ConnectionManager(
factory,
jdbcContext,
connectionReleaseMode,
interceptor,
ois.readBoolean(),
ois.readBoolean()
);
}
|
public void flushBeginning() {
log.trace( "registering flush begin" );
isFlushing = true;
}
Callback to let us know that a flush is beginning. We use this fact
to temporarily circumvent aggressive connection releasing until after
the flush cycle is complete #flushEnding() |
public void flushEnding() {
log.trace( "registering flush end" );
isFlushing = false;
afterStatement();
}
Callback to let us know that a flush is ending. We use this fact to
stop circumventing aggressive releasing connections. |
public Batcher getBatcher() {
return batcher;
}
The batcher managed by this ConnectionManager. |
public Connection getConnection() throws HibernateException {
if ( isClosed ) {
throw new HibernateException( "connection manager has been closed" );
}
if ( connection == null ) {
openConnection();
}
return connection;
}
Retrieves the connection currently managed by this ConnectionManager.
Note, that we may need to obtain a connection to return here if a
connection has either not yet been obtained (non-UserSuppliedConnectionProvider)
or has previously been aggressively released (if supported in this environment). |
public SessionFactoryImplementor getFactory() {
return factory;
}
|
public boolean hasBorrowedConnection() {
// used from testsuite
return borrowedConnection != null;
}
|
public boolean isAggressiveRelease() {
if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT ) {
return true;
}
else if ( releaseMode == ConnectionReleaseMode.AFTER_TRANSACTION ) {
boolean inAutoCommitState;
try {
inAutoCommitState = isAutoCommit()&& !callback.isTransactionInProgress();
}
catch( SQLException e ) {
// assume we are in an auto-commit state
inAutoCommitState = true;
}
return inAutoCommitState;
}
return false;
}
Will connections be released after each statement execution?
Connections will be released after each statement if either: |
public boolean isAutoCommit() throws SQLException {
return connection == null
|| connection.isClosed()
|| connection.getAutoCommit();
}
Is the connection considered "auto-commit"? |
public boolean isCurrentlyConnected() {
return wasConnectionSupplied ? connection != null : !isClosed;
}
Is this ConnectionManager instance "logically" connected. Meaning
do we either have a cached connection available or do we have the
ability to obtain a connection on demand. |
public boolean isReadyForSerialization() {
return wasConnectionSupplied ? connection == null : !batcher.hasOpenResources();
}
|
public boolean isSuppliedConnection() {
return wasConnectionSupplied;
}
Was the connection being used here supplied by the user? |
public Connection manualDisconnect() {
return cleanup();
}
Manually disconnect the underlying JDBC Connection. The assumption here
is that the manager will be reconnected at a later point in time. |
public void manualReconnect() {
}
Manually reconnect the underlying JDBC Connection. Should be called at
some point after manualDisconnect().
This form is used for ConnectionProvider-supplied connections. |
public void manualReconnect(Connection suppliedConnection) {
this.connection = suppliedConnection;
}
Manually reconnect the underlying JDBC Connection. Should be called at
some point after manualDisconnect().
This form is used for user-supplied connections. |
public void releaseBorrowedConnection() {
if ( borrowedConnection != null ) {
try {
BorrowedConnectionProxy.renderUnuseable( borrowedConnection );
}
finally {
borrowedConnection = null;
}
}
}
|
public void serialize(ObjectOutputStream oos) throws IOException {
oos.writeBoolean( wasConnectionSupplied );
oos.writeBoolean( isClosed );
}
|