Hibernate Cache Mechanism

Source: Internet
Author: User

Level 1 cache (session cache)
1. The life cycle is the life cycle of the session.
2. data stored in the primary cache is private data.
Stores sessions in threadlocal, and different threads cannot be accessed. This ensures data security.
3. How to store data in a level-1 Cache
The session. save/update/load/get method can be stored in the first-level cache.
4. The session. get/load method can be used to retrieve data from the first-level cache.
5. The session. evict method can clear an object from the first-level cache.
6. The session. clear method can be used to clear all data in the session.
7. Use session. Refresh to synchronize data in the database to the cache
8. session. flush
In the session cache, all persistent objects are checked.
1. If a persistence object does not have an ID value, an insert statement will be issued.
2. If a persistent object has an ID value, it will check the snapshot for comparison. If the same, nothing will be done. If not, an update statement will be issued.
3. check whether all persistent objects have associated objects.
Check cascade operations of associated objects
Check the link operation of the associated object
9. batch operation


Java sample code

/*** Session. the get method stores data in the first-level cache * // *** session. the load method stores data in the first-level cache * // *** session. the save method saves the data in the first-level cache * // *** session. the update method saves data in the first-level cache */@ Testpublic void testUpdate () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); Classes classes = (Classes) session. get (Classes. class, 1L); session. evict (classes); // The classes object clears the session from the session. update (classes); // put the classes object into the session cache. classes = (Classes) session. get (Classes. class, 1L); transaction. commit ();}/*** session. clear */@ Testpublic void testClear () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); Classes classes = (Classes) session. get (Classes. class, 1L); session. clear (); // The classes object clears the classes = (Classes) session from the session. get (Classes. class, 1L); transaction. commit () ;}@ Testpublic void testClearTest () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); Classes classes = (Classes) session. get (Classes. class, 1L); session. clear (); // if this statement is not added, two different objects have the same ID value. Therefore, you must clear one of them as Classes classes2 = new Classes (); classes2.setCid (1L); classes2.setCname ("asfd"); session. update (classes2); transaction. commit ();}/*** refresh the data in the database to the cache */@ Testpublic void testRefresh () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); Classes classes = (Classes) session. get (Classes. class, 1L); classes. setCname ("66"); session. refresh (classes); // fl the cid value 1 from the database to the cache System. out. println (classes. getCname (); transaction. commit ();}/*** session. flush */@ Testpublic void testFlush () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); Classes classes = (Classes) session. get (Classes. class, 1L); classes. setCname ("afdsasdf"); Set
 
  
Students = classes. getStudents (); for (Student student: students) {student. setDescription ("asdf");} session. flush (); transaction. commit ();}/*** insert 1 million pieces of data into the database */@ Testpublic void testSaveBatch () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); for (int I = 6; I <1000000; I ++) {Classes classes = new Classes (); classes. setCname ("aaa"); classes. setDescription ("afds"); session. save (classes); // if you do not use this judgment, memory overflow will occur, // because of session. the save () operation will persistently store the object in the memory if (I % 50 = 0) {session. flush (); session. clear () ;}} transaction. commit ();}/*** session. flush only sends an SQL statement and does not clear the session cache */@ Testpublic void testFlush2 () {Session session = sessionFactory. getCurrentSession (); Transaction transaction = session. beginTransaction (); Classes classes = (Classes) session. get (Classes. class, 1L); session. flush (); classes = (Classes) session. get (Classes. class, 1L); transaction. commit ();}
 

Secondary cache:

Level 2 Cache: stores Public Data
1. Applicable scenarios:
1. data cannot be updated frequently
2. data can be made public, and privacy is not strong
2. hibernate does not provide a second-level cache solution.
3. The implementation of level-2 caching relies on third-party vendors.
Ehcache
Oscache
Jbosscache
Swamchache
4. Secondary cache operations
1. The second-level cache exists in sessionFactory.
2. lifecycle: Consistent with sessionFactory

3. Step 1 of using second-level cache.
 
  
True
                   
 
  
Org. hibernate. cache. EhCacheProvider
 2. Set an object to the second-level cache * In hibernate. cfg. xml
 * In the ing File
 3. Use session. get/session. load



Query cache:

The query cache is based on the amount of the second-level cache. We must configure the second-level cache first.

Fill in the following configuration in hibernate. cfg. xml:

True


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.