Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate caching?

I'm using Struts 2 and Hibernate. I have a simple table with a Date field that stores information about when a certain action took place. This date value is displayed in my jsp. The problem I have is that after the hibernate updates the db, the jsp page does not update the date value. As a working example:

date1 = 22/06/11 15:00:00 
date2 = 22/06/11 16:00:00

When I refresh manually (F5) then it's OK - the date value changes from date1 to date2 (i.e. from 15:00 to 16:00). But if I keep refreshing, then the jsp will once show date1 and next time date2 and so on. I have the following in my hibernate.cfg:

<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>

I experimented with Hibernate's evict(), flush(). I tried adding a scriptlet (yes, I know - scriptlets are bad practice):

<%
   response.setHeader( "Pragma", "no-cache" );
   response.setHeader( "Cache-Control", "no-cache" );
   response.setDateHeader( "Expires", 0 );
%>

I'm a bit stuck here - any help appreciated.

Thanks, Damo

EDIT: I have a DaoEngine class, which all my DAOs extend.

public class DaoEngine
{

    @SuppressWarnings("unchecked")
    private static final ThreadLocal session = new ThreadLocal();
    private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

    protected DaoEngine()
    {
    }

    @SuppressWarnings("unchecked")
    public static Session getSession()
    {
        Session hibSession = (Session) DaoEngine.session.get();
        if (hibSession == null)
        {
            hibSession = sessionFactory.openSession();
            DaoEngine.session.set(hibSession);
        }
        return hibSession;
    }

    protected void begin()
    {
        getSession().beginTransaction();
    }

    protected void commit()
    {
        getSession().getTransaction().commit();
    }

    @SuppressWarnings("unchecked")
    protected void rollback()
    {
        try
        {
            getSession().getTransaction().rollback();
        }
        catch (HibernateException e)
        {
        }
        try
        {
            getSession().close();
        }
        catch (HibernateException e)
        {
        }
        DaoEngine.session.set(null);
    }

    @SuppressWarnings("unchecked")
    public static void close()
    {
        getSession().close();
        DaoEngine.session.set(null);
    }

    public void clearAll()
    {
        getSession().clear();
    }
}
like image 391
damo_inc Avatar asked Oct 11 '22 16:10

damo_inc


1 Answers

But if I keep refreshing, then the jsp will once show date1 and next time date2 and so on.

I assume you mean that as you refresh, the output is switching between stale and fresh data?

If so, this is usually caused by a failure to close and remove the Hibernate session from your ThreadLocal. Most application servers will reuse threads from a thread pool to process requests, so if an old Session isn't removed from the ThreadLocal, then it will be reused and its persistence context will be out of sync with the database.

Be absolutely sure that you are calling the close() method on your DaoEngine class prior to the end of the request. You should probably set up and tear down sessions in a servlet filter or a Struts2 interceptor.

like image 182
Steven Benitez Avatar answered Oct 13 '22 21:10

Steven Benitez