org.hibernate.Session#isConnected ( )源码实例Demo

下面列出了org.hibernate.Session#isConnected ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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();
}
 
/**
 * 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();
}
 
源代码3 项目: lams   文件: HibernateTransactionManager.java
/**
 * 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();
}
 
/**
 * 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();
}
 
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);
        }
    }
}
 
源代码6 项目: lams   文件: HibernateTransactionManager.java
@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();
}
 
@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();
}