Hibernate's first level, level two buffering for hibernate

Source: Internet
Author: User


"Hibernate" level, level two buffering


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,

@Test publicvoid Findtestyijihuanchong () {Session s=sessionfactory.opensession ();       S.begintransaction ();       Person person= (person) s.load (Person.class, 1);       System.out.println (Person.getname ());       Because the session buffer, so this query directly in the session to take person person2= (person) s.load (Person.class, 1);       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, the following situation, the newly opened Session it? 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.

@Test     publicvoidfindtestyijihuanchong () {         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 ();     } 


Second-level buffering




What is a level two buffer? Second-level buffering can also be understood assessionfactorylevel of buffering,sessionfactoryis the productionSessionFactory, then can we understand thatSessionassociating a result set that points to a database, the next time ISQLtime, I found thatsessionfactorythere is already a statement pointing to this result set, then I can not use it directly!


Specifically, a level two buffer is not Hibernate to provide, is a buffer plug-in provided by a third party, usually has the following third-party buffer plug-ins:

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

Oscache : Available 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 for Hibernate provides support for query caching.

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

JBossCache : Can be used as a cluster-wide cache, support transactional concurrency access policies, Hibernate provides support for 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 requires frequent access to the database, which is certainly well suited to put in buffering

2. rarely concurrency data, what does that 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 that's 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 of all we need to be in our Hibernate.cfg.xml of our two-level buffer, and of course it could be Properties Configuration in Files

<!--turn on the buffer--><property name= "Hibernate.cache.use_second_level_cache" >true</property><!-- Specifies which level two buffer--><property name= "Hibernate.cache.provider_class" >org.hibernate.cache.ehcacheprovider</ property><!--Use Query level two buffer--><propertyname= "Hibernate.cache.use_query_cache" >true</property>

In the second step, we specify which entity class needs a level two buffer

Annotations Configuration

@Entity @cache (usage=cacheconcurrencystrategy.read_write) @Table (name= "P_person")

XML configuration

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

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

must also 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

@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 hibernatethat the query bufferwill not take effect until the query buffer is used.

Setcacheable (True)

OK, look at the code.

@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 ());         }&nbSp;       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.



This article from "Promise always attached to the small wood 、、、" blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1577057

Hibernate's first level, level two buffering for 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.