Example usage for org.hibernate Session disconnect

List of usage examples for org.hibernate Session disconnect

Introduction

In this page you can find the example usage for org.hibernate Session disconnect.

Prototype

Connection disconnect();

Source Link

Document

Disconnect the session from its underlying JDBC connection.

Usage

From source file:org.beangle.commons.orm.hibernate.HibernateTransactionManager.java

License:Open Source License

@Override
protected void doCleanupAfterCompletion(Object transaction) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

    // Remove the session holder from the thread.
    if (txObject.isNewSessionHolder()) {
        TransactionSynchronizationManager.unbindResource(getSessionFactory());
    }//from   w ww.j ava  2 s.c  om

    // 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 {
            @SuppressWarnings("deprecation")
            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 [" + SessionUtils.toString(session) + "] after transaction");
        }
        SessionUtils.closeSession(session);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Not closing pre-bound Hibernate Session [" + SessionUtils.toString(session)
                    + "] after transaction");
        }
        if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
            session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
        }
        if (!this.hibernateManagedSession) {
            session.disconnect();
        }
    }
    txObject.getSessionHolder().clear();
}

From source file:org.beangle.orm.hibernate.BeangleSessionContext.java

License:Open Source License

public void beforeCompletion() {
    Session session = this.sessionHolder.getSession();
    if (this.sessionHolder.getPreviousFlushMode() != null) {
        // In case of pre-bound Session, restore previous flush mode.
        session.setFlushMode(this.sessionHolder.getPreviousFlushMode());
    }//w w  w.j av a  2s .c  om
    // Eagerly disconnect the Session here, to make release mode "on_close" work nicely.
    session.disconnect();
}

From source file:org.beangle.orm.hibernate.HibernateTransactionManager.java

License:Open Source License

@Override
protected void doCleanupAfterCompletion(Object transaction) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;

    // Remove the session holder from the thread.
    if (txObject.isNewSessionHolder()) {
        TransactionSynchronizationManager.unbindResource(getSessionFactory());
    }/* w  ww. j a v  a  2s  .  c  om*/

    // 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 = ((SessionImplementor) session).connection();
            DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
        } catch (HibernateException ex) {
            logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
        }
    }

    if (txObject.isNewSession()) {
        SessionUtils.closeSession(session);
    } else {
        if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
            session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
        }
        if (!this.hibernateManagedSession)
            session.disconnect();

    }
    txObject.getSessionHolder().clear();
}

From source file:org.codehaus.groovy.grails.orm.hibernate.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    try {//  w  w  w .j a v  a  2s.  c o m
        final boolean isWebRequest = request.getAttribute(IS_FLOW_REQUEST_ATTRIBUTE,
                WebRequest.SCOPE_REQUEST) != null;
        if (isWebRequest) {
            return;
        }

        request = (WebRequest) RequestContextHolder.currentRequestAttributes();
        if (!(request instanceof GrailsWebRequest)) {
            super.afterCompletion(request, ex);
            return;
        }

        GrailsWebRequest webRequest = (GrailsWebRequest) request;
        HttpServletResponse response = webRequest.getCurrentResponse();
        if (!(response instanceof GrailsContentBufferingResponse)) {
            super.afterCompletion(request, ex);
            return;
        }

        GrailsContentBufferingResponse bufferingResponse = (GrailsContentBufferingResponse) response;
        // if Sitemesh is still active disconnect the session, but don't close the session
        if (!bufferingResponse.isActive()) {
            super.afterCompletion(request, ex);
            return;
        }

        try {
            Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
            if (session != null) {
                session.disconnect();
            }
        } catch (IllegalStateException e) {
            super.afterCompletion(request, ex);
        }
    } finally {
        AbstractSavePersistentMethod.clearDisabledValidations();
    }
}

From source file:org.firstopen.singularity.util.HibernateUtilImpl.java

License:Apache License

/**
 * Disconnect and return Session from current Thread.
 * /*from   w  w w.j a  va2  s.  com*/
 * @return Session the disconnected Session
 */
public Session disconnectSession() {
    SessionFactoryManager sfm = getSessionFactoryManager();
    Session session = getSession();
    try {
        sfm.threadSession.set(null);
        if (session.isConnected())
            if (session.isOpen())
                session.disconnect();
    } catch (HibernateException ex) {
        throw new InfrastructureException(ex);
    }
    return session;
}

From source file:org.fornax.cartridges.sculptor.framework.web.hibernate.DisconnectHibernatePhaseListener.java

License:Apache License

public void afterPhase(PhaseEvent event) {
    Map<String, Object> requestMap = event.getFacesContext().getExternalContext().getRequestMap();
    if (requestMap.containsKey(HIBERNATE_SESSION_ATTRIBUTE)) {
        Session hibernateSession = (Session) requestMap.get(HIBERNATE_SESSION_ATTRIBUTE);
        if (hibernateSession != null) {
            requestMap.remove(HIBERNATE_SESSION_ATTRIBUTE);
            if (hibernateSession.isConnected()) {
                hibernateSession.disconnect();
            }/*from   w w w  .j  a v a  2 s  .co m*/
        }
    }
}

From source file:org.fornax.cartridges.sculptor.framework.web.hibernate.OpenHibernateSessionInConversationListener.java

License:Apache License

@Override
public void paused(RequestContext context) {
    if (isPersistenceContext(context.getActiveFlow())) {
        Session hibernateSession = getHibernateSession(context);
        unbind(hibernateSession);/*from   w w  w .java  2 s  .  c  o  m*/
        hibernateSession.disconnect();
    }
}

From source file:org.fornax.cartridges.sculptor.framework.web.hibernate.OpenHibernateSessionInConversationListener.java

License:Apache License

@Override
public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap output) {
    if (isPersistenceContext(session.getDefinition())) {
        if (session.getScope().contains(HIBERNATE_SESSION_ATTRIBUTE)) {
            // the hibernate session was created by this flow, and will be
            // closed by this flow
            Session hibernateSession = (Session) session.getScope().remove(HIBERNATE_SESSION_ATTRIBUTE);
            unbind(hibernateSession);/*  w  w  w. j a  va  2  s .  co m*/
            hibernateSession.close();
        } else {
            Session hibernateSession = getHibernateSession(context);
            unbind(hibernateSession);
            hibernateSession.disconnect();
        }
    }
}

From source file:org.fornax.cartridges.sculptor.framework.web.hibernate.OpenHibernateSessionInConversationListener.java

License:Apache License

@Override
public void exceptionThrown(RequestContext context, FlowExecutionException exception) {
    if (isPersistenceContext(context.getActiveFlow())) {
        Session hibernateSession = getHibernateSession(context);
        unbind(hibernateSession);/*from  w w  w.j  av  a2 s . co m*/
        hibernateSession.disconnect();
    }
}

From source file:org.grouter.common.hibernate.HibernateUtil.java

License:Apache License

/**
 * Disconnect and return Session from current Thread.
 *
 * @return Session the disconnected Session
 * @throws InfrastructureException if failure to retrieve session from SessionFactory
 *//*from w  ww.  j a  v a2s  . co  m*/
public static Session disconnectSession() throws InfrastructureException {
    Session session = getSession();
    try {
        THREADSESSION.set(null);
        if (session.isConnected() && session.isOpen()) {
            session.disconnect();
        }
    } catch (HibernateException ex) {
        throw new InfrastructureException(ex);
    }
    return session;
}