These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Displaying old data - caching problem?
PostPosted: Tue Jun 21, 2005 10:40 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Hibernate version: 3.0.5 (using EHCache)
Name and version of the database you are using: MySQL 4.1.12

I have a Struts application running with Hibernate and a strange problem persuing me for quite a while.

Example:
I have a class "Subject" with two attributes, id (Long) and name (String).
I can add, update and delete these subjects. Having a look at the database everything is working fine - subject get added, update and deleted.

When I try to edit a subject and change its name the result is fine in the database as well, but the browser is displaying the old name. Hitting the reload button displays the new name. Well, the strange thing is that hitting the reload button once again shows the old name again and so on...

I don't know, if this is a Struts issue, though I hope you can help me.
By the way, the problem still exists when I disable EHCache.

Action for editing a subject
Code:
public class SubjectEditAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // get a Hibernate session and begin a transaction
        final Session session = HibernateUtil.getSession();
        HibernateUtil.beginTransaction();

        // get the form bean
        final SubjectBean subjectBean = (SubjectBean)form;

        // update the subject
        final Subject subject = (Subject)session.load(Subject.class, subjectBean.getSelectedSubject());
        subject.setName(subjectBean.getName());

        // commit the transaction
        HibernateUtil.commitTransaction();
        session.flush();

        // forward to the success page
        return mapping.findForward("success");
    }
}


Action for listing all subjects
Code:
public class SubjectListAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // get a Hibernate session and begin a transaction
        final Session session = HibernateUtil.getSession();
        HibernateUtil.beginTransaction();

        // create a Hibernate query to select the results from the database
        final StringBuilder queryString = new StringBuilder().
                append("SELECT s FROM Subject AS s ").
                append("ORDER BY s.name ASC");
        final Query query = session.createQuery(queryString.toString());
        final List elements = query.list();
        Subject[] subjects = null;
      
      // something else is done here...
      // "subjects" is filled with elements

        // commit the transaction
        HibernateUtil.commitTransaction();
        session.flush();

        // put Subject objects into bean
        final SubjectBean subjectBean = (SubjectBean)form;
        subjectBean.setSubjects(subjects);

        // forward to the success page
        return mapping.findForward("success");
    }
}


In order to exclude any browser caching issues, every JSP has the following lines in the header:
Code:
response.setHeader("Cache-Control","no-cache"); // Forces caches to obtain a new copy of the page from the origin server
response.setHeader("Cache-Control","no-store"); // Directs caches not to store the page under any circumstance
response.setDateHeader("Expires", 0); // Causes the proxy cache to see the page as "stale"
response.setHeader("Pragma","no-cache"); // HTTP 1.0 backward compatibility


If you need any further information, please tell me.

Thanks a lot in advance!

Best regards
Sven


Top
 Profile  
 
 Post subject: More information
PostPosted: Wed Jun 22, 2005 8:20 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Here is more information about my configuration.

I hope someone is able to help me...

Best regards
Sven


Hibernate mapping Subject.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="de.app.persistence.Subject" table="subjects" dynamic-update="true">
        <id name="id" column="id" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>

        <property name="name" column="name" type="string" length="128" not-null="true" unique="true"/>
    </class>
</hibernate-mapping>


Excerpt from struts-config.xml
Code:
<action path="/listSubjects"
      type="de.app.webapp.struts.actions.SubjectListAction"
      name="subjectBean"
      scope="request"
   validate="false">
   <forward name="success" path="/jsp/admin_subject_list.jsp"/>
</action>

<action path="/loadSubject"
      type="de.app.webapp.struts.actions.SubjectLoadAction"
      name="subjectBean"
      scope="request"
   validate="false">
   <forward name="success" path="/jsp/admin_subject_edit.jsp"/>
</action>

<action path="/editSubject"
      type="de.app.webapp.struts.actions.SubjectEditAction"
      name="subjectBean"
      scope="request"
      input="/jsp/admin_subject_edit.jsp"
   validate="true">
   <forward name="error"   path="/general_error_page.jsp"/>
   <forward name="success" path="/jsp/admin_subject_edit.jsp?success=true"/>
</action>


ehcache.xml
Code:
<ehcache>
    <diskStore path="java.io.tmpdir"/>

    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <cache name="org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory="5"
        eternal="false"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <cache name="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory="5000"
        eternal="true"
        overflowToDisk="true"
        />
</ehcache>


Top
 Profile  
 
 Post subject: Re: Displaying old data - caching problem?
PostPosted: Mon Oct 09, 2006 7:10 am 
Newbie

Joined: Mon Oct 09, 2006 6:58 am
Posts: 2
I hava the same problem as you have had a year ago. I hope you had solved the problem, and know the solution which can help me.
I test some solutions: disabling 2nd level cache, clearing session, ....

I use Hibernate 3.1.

when I update a field in DB, the page does not show the new values and with referesh button it shows strange behaviour: sometimes values are correct, sometime are previous values, sometimes are older values!!!

Thanks beforehand,
sama


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 1:25 am 
Beginner
Beginner

Joined: Thu Oct 06, 2005 8:14 pm
Posts: 23
We had a similar issue a couple of months back
when we delete something there is nothing in the db but it keeps it in its second level cache so we do a retrieve as well when we delete forcing hibernate to go to the db and get a fresh copy.....
Might not be totally related to your issue


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.