Hibernate Timestamp Cache Area

Source: Internet
Author: User

Timestamp: A point in time that is detailed to the second, is a string of seconds and seconds

The timestamp cache area holds the timestamp of the INSERT, update, or delete operation for the table associated with the query results.
Hibernate determines whether the cached query results expire by using the timestamp cache area.
The operation process is as follows: (T1 and T2 do not stipulate who first who after, is the program need to operate)
T1 time to execute the query operation (for example, a customer 1 in the client to make a query), the query results are stored in the Querycache area, the timestamp of the region is recorded as T1
T2 time to update the table related to the query results (the administrator has updated the table of the client query at some time), Hibernate puts T2 time in the Updatetimestampcache area.
T3 time to execute the query results (another client executed the same query statement as the customer 1), compare the timestamp of the Querycache Zone (T1) and the Updatetimestampcache region's timestamps (T2),
If T2 >t1, then discard the original query results stored in the Querycache area, re-query the data in the database, and then store the results in the Querycache region;
If T2 < T1, get query results directly from Querycache
This is how the timestamp is cached:

The query cache for all level two caches follows the timestamp cache policy, the object level two cache, and the collection two level cache do not support this policy.

For example, we have a level two cache for an object, but we do update the object table.
The test code is as follows

@Test Public voidTesthibernatesecondlevelcache () {Employee Employee= (employee) session.get (employee).class, 15);                 System.out.println (Employee.getname ());        Transaction.commit ();        Session.close ();        System.out.println ("--------------------------------------"); Session=sessionfactory.opensession (); Transaction=session.begintransaction (); Employee Employee2=NewEmployee (); Employee2.setemail ("[Email protected]"); Employee2.setname ("Jeremy");        Employee2.setsalary (8000F);                        Session.save (EMPLOYEE2); Employee employee3= (employee) session.get (employee).class, 15);     System.out.println (Employee3.getname ()); }

I have set the level two cache, if the level two cache does not expire I'm supposed to be sending a SELECT statement and an INSERT statement, and that's how it works.

Hibernate:     select        employee0_.id as id1_1_0_,        employee0_.name as name2_1_0_,        employee0_. SALARY as salary3_1_0_,        employee0_. EMAIL as email4_1_0_,        employee0_. dept_id as dept_id5_1_0_     from        gg_employee employee0_     where        employee0_.id=?  AA--------------------------------------Hibernate:     insert     into        gg_employee        ( NAME, SALARY, EMAIL, dept_id)     values        (?,?,?,?) AA

It also proves that the object level two cache does not support timestamp caching policies. And so is collection,

This policy is supported by the query cache:

The code is as follows:

For example, run the following test code:
@Test public    void Testquerycache () {        query query = Session.createquery ("from Employee e where e.id=1"); C5/>query.setcacheable (True);                list<employee> emps = query.list ();        System.out.println (Emps.size ());        System.out.println (Emps.iterator (). Next (). GetClass ());                Employee Employee=new Employee (), Employee.setemail ("[Email protected]") Employee.setname ("Jeremy") ; Employee.setsalary (8000F); Session.save (employee); Emps = query.list (); System.out.println (Emps.size ()); Criteria = Session.createcriteria (Employee.class); Criteria.setcacheable (TRUE); }

Operation Result:

Hibernate:     Select        employee0_.id as id1_1_,        employee0_.name as name2_1_,        employee0_. SALARY as salary3_1_,        employee0_. EMAIL as email4_1_,        employee0_. dept_id as dept_id5_1_     from        gg_employee employee0_     where        employee0_.id=101class  Com.atguigu.hibernate.entities.EmployeeHibernate:     insert     into        gg_employee        (NAME, SALARY, EMAIL, dept_id)     values        (?,?,?,?) Hibernate:     Select        employee0_.id as id1_1_,        employee0_.name as name2_1_,        employee0_. SALARY as salary3_1_,        employee0_. EMAIL as email4_1_,        employee0_. dept_id as dept_id5_1_     from        gg_employee employee0_     where        employee0_.id=101 


I made the first HQL query and then added the data table, the data table has changed, the query cache is closed (even if the update operation on my HQL statement query results have no effect, but the query cache is closed)

If the query cache is not closed, it is already sending a SELECT statement and an INSERT statement, but now it is sending two SELECT statements, an INSERT statement, so it proves that when the query cache related tables are updated,The query cache is automatically closed, which you need to remember

Hibernate Timestamp Cache Area

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.