Hibernate manual Update data query data is not synchronized with update data

Source: Internet
Author: User
Tags commit

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.