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

The following examples show how to use org.hibernate.Session#isConnected() . These examples are extracted from open source projects. 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
/**
 * 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
/**
 * 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
/**
 * 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
/**
 * 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
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
@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
@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();
}