Recently in the project, two-level password verification has been a problem, the original use of Ajax asynchronous commit verification, the program just started to run when there is no problem, but once the user modifies the level two password, when the need to enter a level two password will always be verified unsuccessful, the resulting password is always updated before the data, Looked for a long time to find out that the original is due to the problem of caching, because jquery and Ajax itself there is a certain cache, Hibernate also has a level and level two cache, so that the manual update after the data has not been synchronized with the query data.
Workaround:
The first step is to make jquery ajax not cache
On the page, add between
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
Inside an action or servlet, add
Response.setheader ("Cache-control", "No-cache, must-revalidate");
Response.setheader ("Pragma", "No-cache");
Response.setdateheader ("Expires", 0);
The second step is to keep the session data synchronized after hibernate data is updated.
When performing hibernate updata or save, be sure to use the transaction, and be sure to commit ();
public void Update (Member transientinstance) {
Session session = GetSession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
Session.update (transientinstance);
Tx.commit ();
} catch (RuntimeException re) {
Tx.rollback ();
throw re;
}finally{
Session.close ();
}
}
And in the query, it is best to call the session of the Clear method;
Session.clear ();
Step three cancel Hibernate's level two cache
Add a property to the Hibernate.cfg.xml
<property name= "Hibernate.cache.use_second_level_cache" >false</property>
Restart Tomcat to run again after the operation is complete.