Hibernate bulk Data Insertion optimization------Insert 10,000 data with Hibernate, what's the best way to optimize it?

Source: Internet
Author: User
Tags commit flush stmt
Hibernate batch processing:

Hibernate batch processing actually from the performance considerations, it is very undesirable, wasting a lot of memory. From its mechanism, hibernate first detects the qualifying data, puts it in memory, and then operates. Actual use down performance is very unsatisfactory, in the author's actual
The use of the following third optimization scheme data is: 100,000 data inserted into the database, the configuration of the mainstream desktop, it takes about 30 minutes, oh, fainted.

There are three ways to address performance issues:

1: Bypassing Hibernate API, directly through the JDBC API to do, this method is better performance. is also the fastest.

2: Use stored procedures.

3: Still use hibernate API to do regular batch processing, can also change, change in, we can find out a certain amount of time, the timely processing of these data will be

Delete, Session.flush (); Session.evict (xx object set); This can also save a bit of performance loss. This "certain amount" should be based on the actual situation to do quantitative reference. Generally about 30-60, but the effect is still not ideal.





1: Bypass Hibernate API, directly through the JDBC API to do, this method is better performance, but also the fastest. (Instance is update operation)



Transaction tx=session.begintransaction (); Note that the hibernate transaction boundary is used

Connection conn=session.connection ();

PreparedStatement stmt=conn.preparedstatement ("Update CUSTOMER as C set c.sarlary=c.sarlary+1 where c.sarlary>1000" );

Stmt.excuteupdate ();

Tx.commit (); Note that the hibernate transaction boundary is used

In this applet, it is very efficient to call the JDBC API directly to access the database. Avoids the performance issues that hibernate first queries to load into memory before operation



2: Use stored procedures. However, this approach takes into account the convenience of Ishi and program deployment and is not recommended for use. (Instance is update operation)

If the underlying database (such as Oracle) supports stored procedures, you can also perform bulk updates through stored procedures. Stored procedures run directly in the database and are faster. In Oracle Number

A stored procedure named Batchupdatecustomer () can be defined in the library with the following code:


Code content
Create or Replace procedure Batchupdatecustomer (p_age in number) as
Begin
Update CUSTOMERS set age=age+1 where age>p_age;
End


The above stored procedure has a parameter p_age, which represents the age of the customer, and the application can invoke the stored procedure as follows:

Code content
tx = Session.begintransaction ();
Connection con=session.connection ();
String procedure = "{call Batchupdatecustomer (?)}";
CallableStatement cstmt = Con.preparecall (procedure);
Cstmt.setint (1,0); Set the age parameter to 0
Cstmt.executeupdate ();
Tx.commit ();


As seen from the above program, the application must also bypass the Hibernate API and invoke the stored procedure directly through the JDBC API.

3: Still use hibernate API to do regular batch processing, can also change, change in, we can find out a certain amount of time, the timely processing of these data will be

Delete, Session.flush (); Session.evict (xx object set); This can also save a bit of performance loss. This "certain amount" should be based on the actual situation to do quantitative reference.

(instance is a save operation)

The business logic is: We want the database to insert 10 0000 data

Tx=session.begintransaction ();

for (int i=0;i<100000;i++)

{

Customer Custom=new customer ();

Custom.setname ("user" +i);

Session.save (custom);

if (i%50==0)//With every 50 data as a processing unit, which is what I said "a certain amount", this amount is to be considered as appropriate

{

Session.flush ();

Session.clear ();

}

}

This will keep the system in a stable range ....

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.