Concerning the Hibernate cache, it is troublesome to connect session. clear to the latest data.

Source: Internet
Author: User
Concerning the Hibernate cache, it is troublesome to connect session. clear to the latest data.

Concerning the Hibernate cache, it is troublesome to connect session. clear to the latest data.

I. hibernate primary cache
(1) hibernate supports two levels of caching. by default, only one level of caching is supported;
(2) each Session has a primary cache;
(3) when a Session is closed, its corresponding level-1 cache is automatically cleared;
(4) the save, update, saveOrupdate, load, get, list, iterate, and lock methods all store objects in the cache.
(5) only get, load, and iterate data can be read from the cache.
(6) the Query object does not read the cache by default. to enable caching, use the syntax:
Query. setCacheable (true );
True
(7) after the query cache is enabled, the cache will match successfully only when the query conditions are exactly the same as those of the previous query.
(8) Criteria does not support caching;
(9) the number of objects in the cache cannot be controlled in the first-level cache. Note that memory overflow may occur when a large volume of data is operated. you can clear the cache.

Session. clear () clear all objects in the cache session. evict (user) clear the specified object

I have tried it. session. clear () can clear session. createQuery, but cannot clear session. createSQLQuery.
Session. createSQLQuery cannot be cleared. I have tried the following methods. if there is no such method, the data is still wrong, not the latest data in the database:

Session. setFlushMode (FlushMode. AUTO); session. setCacheMode (CacheMode. NORMAL); session. flush (); session. clear (); sqlQuery. setCacheable (false );

The only method that can be cleared is to close the session... Speechless, does hibernate mean SB, or does it not work if I am SB? Is there a dedicated cache for session. createSQLQuery? Is it a BUG that the cache cannot be cleared?

II. hibernate secondary cache

The save, update, saveOrupdate, load, get, list, query, and Criteria methods all fill the second-level cache with get, load, and iterate to retrieve data sessions from the second-level cache. save (user) if the user primary key is generated using "native", it is not placed in the second-level cache.

(1) enable Level 2 cache

True

(2) specify the implementation class of level 2 cache for hibernate

Org

(3) Create a configuration file for the OSCache cache (hibernate_Advance_Surpport_lib is required)
Src/oscache. properties
Modify the configuration:
Cache. capacity = 1000 specify how many objects the cache can hold

(4) specify the classes that need to be placed in the second-level cache, and the objects that need to be used for a long time must be placed in the second-level cache.

// Objects in the cache cannot be updated. // Allow update of objects in the cache

Or in an orm file:

...

(5) to clear the second-level cache, use the following syntax:

SessionFactory. evict (User. class) clear all users

Getting started with Hibernate

Enable logging in Hibernate

Hibernate + JUnit test object class generation database table

Hibernate overall understanding

Hibernate ing mechanism

Hibernate details: click here
Hibernate: click here

This article permanently updates the link address:

Related Article

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.