Hibernate batch-size hibernate. JDBC. batch_size

Source: Internet
Author: User

It took a day to test the attributes of batch-size and hibernate. JDBC. batch_size.

First, let's talk about the attribute hibernate. JDBC. batch_size.

The cucutebatch () method... submits several SQL statements together to improve the performance. hibernate. JDBC. batch_size is set in hibernate. cfg. xml.

Batch import data code.

// For (INT I = 0; I <25; I ++ ){
// Userinfo u = new userinfo ();
// U. setusername ("dengyin" + I );
// U. setpassword ("sdfa ");
// U. setemail ("dasfa ");
// Session. Save (U );
// If (I % 25 = 0 ){
// Session. Flush (); // flush session and clear cache for every 25 logs. This avoids memory overflow (if there are many data structures)
// Session. Clear (); // clear Cache
//}
//
//}

The batch-size can be set in the class and set definition of hBM. At first, I thought that batch-size is the number of children, but actually the number of parents is actually obtained.

But the strange thing is that when I test the batch-size value from 1 to 5, when it is set to 1 and 2, it is actually a parent each time. when 3, three parents are obtained simultaneously. when the selected parent is exceeded, it seems that there is no rule. I recommend that you set batch-size to 3.

The following is from the hibernate document. Introduction to batch fetching .)

Hibernate can make efficient use of batch fetching, that is, Hibernate can load several uninitialized proxies if one proxy is accessed. batch fetching is an optimization for the lazy loading strategy. there are two ways you can tune batch fetching: On the class and the collection level.

Batch fetching for classes/entities is easier to understand. Imagine you have the following situation at runtime: You have 25CatInstances loaded inSession, EachCatHas a reference to itsOwner,Person.PersonClass is mapped with a proxy,Lazy = "true". If you now iterate through all cats and callGetowner ()On each, Hibernate will by default execute 25SelectStatements, to retrieve the proxied owners. You can tune this behavior by specifyingBatch-sizeIn the mappingPerson:

<class name="Person" lazy="true" batch-size="10">...</class>

Hibernate will now execute only three queries, the pattern is 10, 10, 5. you can see that batch fetching is a blind guess, as far as performance optimization goes, it depends on the number of unitilized proxies in a participantSession.

You may also enable batch fetching of collections. For example, if eachPersonHas a lazy collectionCatS, and 10 persons are currently loaded inSesssion, Iterating through all persons will generate 10SelectS, one for every callGetcats (). If you enable batch fetching forCatsCollection in the mappingPerson, Hibernate can pre-Fetch collections:

<class name="Person">    <set name="cats" lazy="true" batch-size="3">        ...    </set></class>

WithBatch-sizeOf 3, Hibernate will load 3, 3, 3, 1 collections in 4SelectS. Again, the value of the attribute depends on the expected number of uninitialized collections in a participantSession.

Batch fetching of collections is special useful if you have a nested tree of items, ie. the typical Bill-of-materials pattern.

I tracked the code.

Net. SF. hibernate. loader. batchingcollectioninitializer public void initialize (serializable ID, sessionimplementor session) method.

Public void initialize (serializable ID, sessionimplementor session)
Throws sqlexception, hibernateexception {
Serializable [] batch = session. getcollectionbatch (collectionpersister, ID, batchsize );
If (smallbatchsize = 1 | batch [smallBatchSize-1] = NULL ){
Nonbatuploader. loadcollection (Session, ID, collectionpersister. getkeytype ());
}
Else if (batch [batchSize-1] = NULL ){
If (log. isdebugenabled () log. debug ("batch loading collection role (small batch):" + collectionpersister. getrole ());
Serializable [] smallbatch = new serializable [smallbatchsize];
System. arraycopy (batch, 0, smallbatch, 0, smallbatchsize );
Smallbatchloader. loadcollectionbatch (Session, smallbatch, collectionpersister. getkeytype ());
Log. debug ("done batch load ");
}
Else {
If (log. isdebugenabled () log. debug ("batch loading collection role:" + collectionpersister. getrole ());
Batchloader. loadcollectionbatch (Session, batch, collectionpersister. getkeytype ());
Log. debug ("done batch load ");
}
}

When smallbatchsize = 1, it is actually a nonbatchloader. loadcollection (Session, ID, collectionpersister. getkeytype. this is the initial ID. I found that when batch-size is 2, smallbatchsize is 1.

Int smallbatchsize = (INT) math. Round (math. SQRT (batchsize); // onetomanypersister. Java 182 rows.

This is the value of smallbatchsize.

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.