Level two cache in hibernate

Source: Internet
Author: User
Tags sessions

Secondary cache usage Scenarios: infrequently modified data, but frequently accessed data is placed in the cache

The first level cache is only the internal cache of the session, used to access SQL statements, such as two consecutive calls to the same parameters of the Get method, is the session cache

The second level cache is the sessiionfactory level of cache, that is, different threads, different programs between the cache

1. Load the configuration in Hibernate.hbm.xml

<property name= "Cache.provider_class" >org.hibernate.cache.HashtableCacheProvider</property>

<!--configuration cache entries can be configured under Hibernate.hbm.xml or under the mapping files under their respective classes.
<class-cache usage= "Read-write" class= "cache. Employee "/>
<class-cache usage= "Read-write" class= "cache. Department "/>

Class Cache

public void Testcahce ()
{
Session session=sessionfactory.opensession ();

Transaction tx=session.begintransaction ();
Employee employee= (Employee) Session.get (Employee.class, 1);
SYSTEM.OUT.PRINTLN (employee);

Session session2=sessionfactory.opensession ();
Employee employee1= (Employee) Session.get (employee.class,1);

Sessionfactory level Two cache when two sessions with Get query the same data will be called using


Tx.commit ();
Session.close ();
}

2. Collection Cache

<collection-cache usage= "Read-write" collection= "cache. Department.employees "/>

/**
* Collection cache in level two cache
*/
@Test
public void TestCahce2 ()
{
Session session=sessionfactory.opensession ();

Transaction tx=session.begintransaction ();
Department department= (Department) Session.get (Department.class, 1);
System.out.println (Department.getemployees ());//Lazy Loading
SYSTEM.OUT.PRINTLN (employee);
Session session2=sessionfactory.opensession ();
Department= (department) Session2.get (Department.class, 1);
System.out.println (Department.getemployees ()); The same data is loaded two times because the collection is set to level two cache
Sessionfactory level Two cache when two sessions with Get query the same data will be called using

Tx.commit ();
Session.close ();

Not with the collection cache


}

Using the collection cache

3. Query statement cache (caching for the same SQL statement only)

(1) Iterator implementation

/**
* Query cache n+1 queries are cached as identical query statements
*/
@Test
public void Testquerycache ()
{
Session session=sessionfactory.opensession ();

Transaction tx=session.begintransaction ();
Iterator<employee> iterator=session.createquery ("from Employee where Id<5"). Iterate ();
while (Iterator.hasnext ())
{
System.out.println (Iterator.next ());
}

Iterator<employee> iterator1=session.createquery ("from Employee where Id<5"). Iterate ();
while (Iterator1.hasnext ())
{
System.out.println (Iterator1.next ());
}
Tx.commit ();
Session.close ();
}

(2) Implement with Setcacheable

Additional configuration needs to be loaded

<property name= "Cache.use_query_cache" >true</property>

@Test
public void Testquerycachetrue ()
{
Session session=sessionfactory.opensession ();

Transaction tx=session.begintransaction ();
List List=session.createquery ("from Employee where Id<5"). Setcacheable (True). List ();
SYSTEM.OUT.PRINTLN (list);
Tx.commit ();
Session.close ();
Session session1=sessionfactory.opensession ();
Transaction tx1=session1.begintransaction ();
List List1=session1.createquery ("from Employee where Id<5"). Setcacheable (True). List ();
System.out.println (List1);
Tx1.commit ();
Session1.close ();

}

But the SQL statement is a bit different, and you can't use the cache

Level two cache in hibernate

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.