[Hibernate] level-1 and level-2 buffering, and hibernate Buffering

Source: Internet
Author: User

[Hibernate] level-1 and level-2 buffering, and hibernate Buffering

Hibernate buffer is divided into two types by level: Session and SessionFactory. Some are also three types, and the other is Query Buffer. Of course, query buffering relies on second-level buffering.

 

OK. What is buffering?

Open up a space in the memory to store the data in the hard disk. In the future, the data will be directly obtained in the memory. This can be simply understood as a buffer.

 

Level 1 Buffer

What is a level-1 buffer? The level-1 buffer is default for Hibernate.

For example, the following code,

1234567891011 @Test    publicvoid findTestyijihuanchong(){       Session s=sessionFactory.openSession();       s.beginTransaction();       Person person=(Person)s.load(Person.class1);       System.out.println(person.getName());       // Because the Session is buffered, this query is directly retrieved from the session.       Person person2=(Person)s.load(Person.class1);       System.out.println(person2.getName());       s.getTransaction().commit();    }

 

We found that only one SQL statement will be issued, so this is the first-level buffer that comes with Hibernate.

 

 

In the following example, what about the new Session? In the system, when multi-thread concurrency occurs, more than one Session will be generated. Therefore, when optimization performance is achieved, the first-level buffer will not meet the requirements, so there will be a second-level buffer.

For example, the following code clearly sends two SQL statements.

12345678910111213141516 @Test    publicvoidfindTestyijihuanchong(){        Sessions=sessionFactory.openSession();        s.beginTransaction();        Personperson=(Person)s.load(Person.class1);        System.out.println(person.getName());        s.getTransaction().commit();        s.close();                 Sessions2=sessionFactory.openSession();        s2.beginTransaction();        Personperson2=(Person)s2.load(Person.class1);        System.out.println(person2.getName());        s2.getTransaction().commit();        s2.close();    }

 

Level 2 Buffer

 

 

 

What is second-level buffer? The second-level buffer can also be understood as the SessionFactory-level buffer, and SessionFactory is the factory that produces the Session, so we can not understand that the Session is associated with a result set pointing to the database, next time I am sending an SQL statement, I found that SessionFactory already has a statement pointing to this result set. Can I use it directly!

 

Specifically, the second-level buffer is not provided by Hibernate, but is provided by a third party. There are usually the following types of third-party buffer plug-ins:

EhCache: it can be used as a process cache. The physical medium for storing data can be memory or hard disk, which provides support for Hibernate query cache.

OSCache: it can be used as a process-specific cache. The physical medium for storing data can be memory or hard disks. It provides a wide range of cache data expiration policies and supports Hibernate query cache.

SwarmCache: it can be used as a cache within the cluster, but does not support Hibernate query cache.

JBossCache: cache within the cluster. It supports transaction-type concurrent access policies and supports Hibernate query cache.

So what data is suitable for second-level buffering? After understanding the second-level buffering feature, we know that,

1. Frequently queried data, which requires frequent access to the database, must be very suitable for caching

2. What does little Concurrent Data mean? For example, a query and a modification may cause a dirty read or phantom read. This means that the data in your database may be modified, but the second-level buffer is not updated in time.

3. Important data.

In short, data stored in the second-level buffer is generally unimportant and infrequently modified. For example, menus, such as permissions. These are very suitable for second-level buffering, such as financial data and wage data. These are not recommended for second-level buffering.

We mentioned above that the second-level buffer is provided by a third party, so we obviously need to configure it,

 

First, we need to enable our second-level buffer in our hibernate. cfg. xml, which may also be configured in the properties file.

123456 <! -- Enable buffering --><property name="hibernate.cache.use_second_level_cache">true</property><! -- Specify the level-2 buffer --><property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><! -- Use query second-level buffer --><propertyname="hibernate.cache.use_query_cache">true</property>

 

Step 2: Specify the object class that requires second-level buffer.

Annotations Configuration

123 @Entity@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)@Table(name="p_person")

XML configuration

123 <class name="Person" table="t_person">        <cache usage="read-write"/>        <id name="id">

Remember that the XML configuration must be before the id, within the class

You must also have an ehcache. xml file. If you are interested in this file, you can check it online. I will not explain it here.

After the configuration, let's look

12345678910111213141516 @Test    publicvoid findTesterjihuanchong(){        Sessions=sessionFactory.openSession();        s.beginTransaction();        Personperson=(Person)s.load(Person.class, 1);        System.out.println(person.getName());        s.getTransaction().commit();        s.close();                 Sessions2=sessionFactory.openSession();        s2.beginTransaction();        Personperson2=(Person)s2.load(Person.class, 1);        System.out.println(person2.getName());        s2.getTransaction().commit();        s2.close();    }

At this time, let's look at it again. It must have sent only one SQL statement.

 

Query Buffer

 

What is Query Buffer. Gu mingyu is the buffer generated during query. What is the relationship between Query Buffer and second-level buffer? First, the query buffer depends on the second-level buffer. The Query Buffer is generally set in the list () method, and the query buffer is the buffer used for repeated queries. If the two queries are different, this buffer does not work. Note that the list () Query Buffer must tell hibernate that the Query Buffer takes effect only when the Query Buffer is used.

 

SetCacheable (true)

OK. Check the code.

1234567891011121314 @Test    publicvoid findTestList(){        Sessions=sessionFactory.getCurrentSession();        s.beginTransaction();        List<Person>persons=s.createQuery("fromPerson").setCacheable(true).list();        List<Person>person1=s.createQuery("fromPerson").setCacheable(true).list();        for(Person person:persons){            System.out.println(person.getName()+"----"+person.getId());        }        for(Person person:person1){            System.out.println(person.getName()+"----"+person.getId());        }        s.getTransaction().commit();    }

 

Here, we have basically completed the Hibernate buffer, but it doesn't mean that how to configure and use the buffer depends on the actual project situation, if the second-level buffer is configured, the system performance will be improved. At the same time, advanced caching algorithms may also be involved. Of course, if you make several more mistakes in the project, you will naturally use the Hibenrate buffer!

 

In the next blog, I will write a pessimistic lock and an optimistic lock. After writing it, I will write an article on how to simulate and develop a set of our own ORM framework. Basically, I have finished writing the common features of Hibenrate, I like to write and play, so you can read it at will and discuss any problems in time.

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.