Example usage for org.hibernate Cache evictAllRegions

List of usage examples for org.hibernate Cache evictAllRegions

Introduction

In this page you can find the example usage for org.hibernate Cache evictAllRegions.

Prototype

default void evictAllRegions() 

Source Link

Document

Evict data from all cache regions.

Usage

From source file:org.cgiar.ccafs.marlo.web.filter.MARLOCustomPersistFilter.java

License:Open Source License

@Override
protected void doFilterInternal(HttpServletRequest httpRequest, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    String url = httpRequest.getRequestURL().toString();
    String queryString = httpRequest.getQueryString();
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(" [").append(httpRequest.getMethod()).append("] ");

    stringBuilder.append(url);//w ww.  j a v a  2s .  c  om
    if (queryString != null) {
        stringBuilder.append("?").append(queryString);
    }

    String requestUrl = stringBuilder.toString();

    try {
        // Create an AuditLogContext for AuditLogging
        AuditLogContextProvider.push(new AuditLogContext());

        LOG.debug("begin doFilter for MARLOCustomPersistFilter for request: " + requestUrl);
        Cache cache = sessionFactory.getCache();

        if (cache != null) {
            cache.evictAllRegions(); // Evict data from all query regions.
        }
        sessionFactory.getCurrentSession().beginTransaction();

        // Continue filter chain
        chain.doFilter(httpRequest, response);

        if (sessionFactory.getCurrentSession() != null
                && sessionFactory.getCurrentSession().getTransaction() != null) {
            sessionFactory.getCurrentSession().getTransaction().commit();

        }

    } catch (StaleObjectStateException staleEx) {
        LOG.error("This interceptor does not implement optimistic concurrency control!");
        LOG.error("Your application will not work until you add compensation actions!");
        httpRequest.getSession().setAttribute("exception", staleEx);
        // Rollback, close everything, possibly compensate for any permanent changes
        // during the conversation, and finally restart business conversation. Maybe
        // give the user of the application a chance to merge some of his work with
        // fresh data... what you do here depends on your applications design.
        throw staleEx;
    } catch (Throwable ex) {

        httpRequest.getSession().setAttribute("exception", ex);
        // Rollback only
        LOG.error("Exception occurred when trying to commit transaction");
        try {
            if (sessionFactory.getCurrentSession().getTransaction().isActive()) {
                LOG.info("Trying to rollback database transaction after exception");
                sessionFactory.getCurrentSession().getTransaction().rollback();
            }
        } catch (Throwable rbEx) {
            LOG.error("Could not rollback transaction after exception!", rbEx);
        }

        // Let others handle it... maybe another interceptor for exceptions?
        throw new ServletException(ex);
    }

    /**
     * We want to decouple or AuditLogInterceptor from our DAOs so we need a mechanism to
     * pass entity values from the DAOs to the Hibernate Interceptors/Hibernate Listeners.
     */
    finally {
        LOG.debug("clean up AuditLogHelper for MARLOCustomPersistFilter request : " + requestUrl);

        // This must get executed in a finally block or otherwise we risk a memory leak.
        AuditLogContextProvider.pop();
    }

}