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 subjectsCode:
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