Java Code Examples for org.hibernate.Session#isConnected()

The following examples show how to use org.hibernate.Session#isConnected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateTransactionManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine whether the given Session is (still) physically connected
 * to the database, that is, holds an active JDBC Connection internally.
 * @param session the Hibernate Session to check
 * @see #isSameConnectionForEntireSession(Session)
 */
protected boolean isPhysicallyConnected(Session session) {
	if (!(session instanceof SessionImplementor)) {
		// The best we can do is to check whether we're logically connected.
		return session.isConnected();
	}
	return ((SessionImplementor) session).getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected();
}
 
Example 2
Source File: HibernateTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Determine whether the given Session is (still) physically connected
 * to the database, that is, holds an active JDBC Connection internally.
 * @param session the Hibernate Session to check
 * @see #isSameConnectionForEntireSession(Session)
 */
protected boolean isPhysicallyConnected(Session session) {
	if (!(session instanceof SessionImplementor)) {
		// The best we can do is to check whether we're logically connected.
		return session.isConnected();
	}
	return ((SessionImplementor) session).getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected();
}
 
Example 3
Source File: HibernateTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine whether the given Session is (still) physically connected
 * to the database, that is, holds an active JDBC Connection internally.
 * @param session the Hibernate Session to check
 * @see #isSameConnectionForEntireSession(Session)
 */
protected boolean isPhysicallyConnected(Session session) {
	if (!(session instanceof SessionImplementor)) {
		// The best we can do is to check whether we're logically connected.
		return session.isConnected();
	}
	return ((SessionImplementor) session).getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected();
}
 
Example 4
Source File: HibernateTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Determine whether the given Session is (still) physically connected
 * to the database, that is, holds an active JDBC Connection internally.
 * @param session the Hibernate Session to check
 * @see #isSameConnectionForEntireSession(Session)
 */
protected boolean isPhysicallyConnected(Session session) {
	if (!(session instanceof SessionImplementor)) {
		// The best we can do is to check whether we're logically connected.
		return session.isConnected();
	}
	return ((SessionImplementor) session).getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected();
}
 
Example 5
Source File: HibernatePersistenceContextInterceptor.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
public void reconnect() {
    if (getSessionFactory() == null) return;
    Session session = getSession();
    if(!session.isConnected() && !disconnected.isEmpty()) {
        try {
            Connection connection = disconnected.peekLast();
            getSession().reconnect(connection);
        } catch (IllegalStateException e) {
            // cannot reconnect on different exception. ignore
            LOG.debug(e.getMessage(),e);
        }
    }
}
 
Example 6
Source File: HibernateTransactionManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
	HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

	// Remove the session holder from the thread.
	if (txObject.isNewSessionHolder()) {
		TransactionSynchronizationManager.unbindResource(getSessionFactory());
	}

	// Remove the JDBC connection holder from the thread, if exposed.
	if (getDataSource() != null) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
	}

	Session session = txObject.getSessionHolder().getSession();
	if (this.prepareConnection && session.isConnected() && isSameConnectionForEntireSession(session)) {
		// We're running with connection release mode "on_close": We're able to reset
		// the isolation level and/or read-only flag of the JDBC Connection here.
		// Else, we need to rely on the connection pool to perform proper cleanup.
		try {
			Connection con = session.connection();
			DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
		}
		catch (HibernateException ex) {
			logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
		}
	}

	if (txObject.isNewSession()) {
		if (logger.isDebugEnabled()) {
			logger.debug("Closing Hibernate Session [" + SessionFactoryUtils.toString(session) +
					"] after transaction");
		}
		SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Not closing pre-bound Hibernate Session [" +
					SessionFactoryUtils.toString(session) + "] after transaction");
		}
		if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
			session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
		}
		if (!this.hibernateManagedSession) {
			session.disconnect();
		}
	}
	txObject.getSessionHolder().clear();
}
 
Example 7
Source File: HibernateTransactionManager.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected void doCleanupAfterCompletion(Object transaction) {
	HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

	// Remove the session holder from the thread.
	if (txObject.isNewSessionHolder()) {
		TransactionSynchronizationManager.unbindResource(getSessionFactory());
	}

	// Remove the JDBC connection holder from the thread, if exposed.
	if (getDataSource() != null) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
	}

	Session session = txObject.getSessionHolder().getSession();
	if (this.prepareConnection && session.isConnected() && isSameConnectionForEntireSession(session)) {
		// We're running with connection release mode "on_close": We're able to reset
		// the isolation level and/or read-only flag of the JDBC Connection here.
		// Else, we need to rely on the connection pool to perform proper cleanup.
		try {
			Connection con = session.connection();
			DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
		}
		catch (HibernateException ex) {
			logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
		}
	}

	if (txObject.isNewSession()) {
		if (logger.isDebugEnabled()) {
			logger.debug("Closing Hibernate Session [" + SessionFactoryUtils.toString(session) +
					"] after transaction");
		}
		SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug("Not closing pre-bound Hibernate Session [" +
					SessionFactoryUtils.toString(session) + "] after transaction");
		}
		if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
			session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
		}
		if (!this.hibernateManagedSession) {
			session.disconnect();
		}
	}
	txObject.getSessionHolder().clear();
}