"Hibernate" level, level two buffering

Source: Internet
Author: User

Hibernate buffering is divided into two categories, one buffer (Session) and two level buffer (sessionfactory), some say three, there is a query buffer, of course, the query buffer is based on the two-level buffer.

OK, what is buffering?

In memory to open a space to the original should have the data inside the hard disk, there is this space inside, in the future, the need for this piece of data directly in memory. This can be simply understood as buffering.

First-level buffering

What is a first-level buffer, the primary buffer is hibernate default, do not have to control it.

such as 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());       //因为Session存在缓冲,所以这个查询直接在session中取       Person person2=(Person)s.load(Person.class1);       System.out.println(person2.getName());       s.getTransaction().commit();    }

We find that only one SQL statement is emitted, so this is the first level of buffering that comes with hibernate.

So for example, what about the new session? In the system, multithreading concurrency, it must not only produce a session, so in the optimization of performance, the primary buffer is often not enough to meet the demand, then there is a level two buffer

The following code, for example, clearly emits 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();    }

Second-level buffering

What is a level two buffer? Second-level buffer can also be understood as sessionfactory level buffer, Sessionfactory is the production session of the factory, then we could understand that the session associated with a point to the database result set, then next time I am in SQL, I found that Sessionfactory There is a statement that points to this result set, then I can not use it directly!

Specifically, level two buffering is not provided by hibernate, but is provided by a third-party buffer plug-in, usually with the following third-party buffers:

EhCache: As a process-wide cache, the physical media that holds the data can be either memory or hard disk, which provides support for Hibernate's query caching.

Oscache: As a process-wide cache, the physical media that holds the data can be either memory or hard disk, providing a rich cache data expiration policy that supports Hibernate's query caching.

Swarmcache: Can be used as a cluster-wide cache but does not support Hibernate's query cache.

JBossCache: Can be used as a cluster-wide cache, supports transactional concurrency access policies, and provides support for hibernate query caching.

So, what data fits in the two-level buffer, and after understanding the two-level buffering feature, we know that

1, frequently queried data, such data need frequent access to the database, is certainly very suitable for buffering

2, rarely concurrent data, what does it mean? For example, a query, a modification, is likely to cause a dirty read, or a phantom read. It means that your database data may have been modified, but the set level two buffer has not been updated in time.

3, important data, this is not much to say

In short, the data placed in level two buffers are generally unimportant and infrequently modified. For example, a menu, such as permissions. These are well suited for two-level buffers, such as financial data, payroll data, and so on, which are not recommended for level two buffers

We're talking about a level two buffer that's provided by a third party. So obviously we need to configure,

First we need to open our level two buffer in our hibernate.cfg.xml, and of course it may be configured in the properties file

123456 <!-- 开启缓冲 --><propertyname="hibernate.cache.use_second_level_cache">true</property><!--指定是哪个二级缓冲--><propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><!-- 使用查询二级缓冲 --><propertyname="hibernate.cache.use_query_cache">true</property>

In the second step, we specify which entity class needs a level two 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" /> &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < id  name = Code class= "XML string" > "id" >

Remember that the XML configuration must be an ID before the class

Also must have Ehcache.xml file, this file is interested you can look at it on the Internet, here I do not explain, the contents of the

After the configuration, we look directly at

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 point, we look at it, and it must be sending only one SQL statement.

Query buffering

What is query query buffering. Gu Mingsi It is the buffer generated by the query, so we are talking about the two-level buffer, what is the relationship between the query buffer and the two-level buffer? First, the query buffer is dependent on the two-level buffer, the query buffer is generally set in the list () method, the query buffer is the buffer used by the repeated query, if you two queries are different, the existence of the buffer is not working. It is important to note that the list () query buffer must tell hibernate that the query buffer will not take effect until the query buffer is used.

Setcacheable (True)

OK, look at 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 basically finished hibernate buffer, but how to configure the buffer, how to use, according to the actual project situation, not to say that the configuration of the two-level buffer will certainly improve the performance of the system. At the same time, the advanced may also involve the buffer algorithm and so on. Of course, more than a few mistakes in the project, will naturally use the hibenrate buffer!

The next post write pessimistic lock and optimistic lock, after writing, I will write an article how to imitate the development of a set of our own ORM framework, basically wrote the common characteristics of hibenrate, idle to Nothing, like to write play, we casually see, there are problems in time to discuss.

"Hibernate" level, level two buffering

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.