Someone once asked gave king what is the most proud of hibernate? Everyone thought that he would say: it is a ing file provided by him, oriented to object-oriented operations.
But he said: the most proud thing is his cache mechanism:
The cache mechanism of hibernate can be classified into Level 1 cache and level 2 cache, and level 1 cache is session level. The second-level cache is at the sessionFactory level.
In the second-level cache, it can be saved not only in the memory, but also on the hard disk. Of course, this cache mechanism is mainly used to operate on a large amount of data. If the data volume is small, you can use a level-1 cache.
I used the EhCache cache. Ehcache is a lightweight cache implementation.
Implement level-2 Cache:
1. Import jar packages; Two: asm-attrs.jar, commons-logging-1.1.1.jar.
2. Add attributes to the main configuration file:
[Java]
<! -- Use level 2 Cache -->
<Property name = "hibernate. cache. use_second_level_cache">
True
</Property>
<Property name = "hibernate. cache. provider_class">
Org. hibernate. cache. EhCacheProvider
</Property>
3. Configure in the ing file: <cache usage = "read-only"/>
Note: There are four values: it is necessary to introduce these four values.
A. transactional: stores repeated transaction isolation levels. This method is usually used for data with A large read/write ratio and few updates.
B. read-write: The timestamp mechanism is used to maintain the isolation level of submitted transactions. This method is usually used for data with a large read/write ratio and few updates.
C. nonstrict-read-write: The second-level cache may be inconsistent with the data in the database. Set a short enough cache expiration time. Otherwise, it is possible to read dirty data from the cache. When some data is rarely changed (the data remains unchanged for one day or two days) in addition, if the data is inconsistent with the cache, this cache policy can be used.
D. read-only: when it is determined that the data will not be changed. We can use this cache policy.
4. Compile the ehcache. xml file. Create a new file in the src directory.
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Ehcache>
<! -- If the memory cannot be stored, put it in a path on the disk -->
<DiskStore path = "d:/ehcache"/>
<! -- Maximum number of objects in memory -->
<DefaultCache maxElementsInMemory = "200" eternal = "false"
TimeToIdleSeconds = "50" timeToLiveSeconds = "60" overflowToDisk = "true"/>
<! -- Saved object -->
<Cache name = "com. test. bean. Student" maxElementsInMemory = "200" eternal = "false"
TimeToIdleSeconds = "50" timeToLiveSeconds = "60" overflowToDisk = "true"/>
</Ehcache>
The configuration is complete.
Then you can test it.
[Java]
Session session1 = sessionfactory. openSession ();
Transaction tx1 = session1.beginTransaction ();
List <Student> list = session. createQuery ("from Student s order by s. name asc"). list ();
For (Student stu: list ){
System. out. println (stu. getName ());
}
Tx1.commit ();
System. out. println ("---------------------------");
Session session2 = sessionfactory. openSession ();
Transaction tx2 = session2.beginTransaction ();
Student s1 = (Student) session2.get (Student. class, "4028ab81386ed77b01386ed77d57000c ");
Student s2 = (Student) session2.get (Student. class, "4028ab81386ed77b01386ed77d570011 ");
System. out. println (s1.getName ());
System. out. println (s2.getName ());
Tx2.commit ();
Enable two sessions and two transactions.
If no second-level cache is configured. It must execute three SQL statements but is configured. You can omit the last two SQL statements.
Over...
Author: yy008871