Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize batch processing in Hibernate

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you how Hibernate implements batch processing. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Hibernate batch processing is actually undesirable from a performance point of view and wastes a lot of memory. From its mechanism, Hibernate first checks out the data that meets the conditions, puts it in memory, and then operates. The actual use of the performance is very unsatisfactory, in the author's actual use of the following third optimization scheme data is: 100000 data inserted into the database, mainstream desktop configuration, takes about 30 minutes, ha ha, faint.

To sum up, there are three ways to solve performance problems:

1: Bypass Hibernate API, directly through JDBC API to do, this method performance is relatively good. It is also the fastest.

2: Use the storage process.

3: Hibernate API is still used for regular batch processing. It can also change. When it changes, it changes. We can find a certain amount of time and complete the operation of these data in time.

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

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

Transaction tx=session.beginTransaction();

//Note that hibernate transaction boundaries are 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 hibernate transaction boundaries are used

This Mini programs, the use of direct calls JDBC API to access the database, high efficiency. Avoid Hibernate query first loaded into memory, and then operation caused by performance problems.

2: Use the storage process. However, considering the ease of planting and program deployment, this method is not recommended. (Example is update operation)

Bulk updates can also be performed through stored procedures if the underlying database (such as Oracle) supports them. Stored procedures run directly in the database and are much faster. Number of Oracle

A stored procedure named batchUpdateCustomer () can be defined in the repository with the following code:

create or replace procedure batchUpdateCustomer(p_age in number)

as begin update CUSTOMERS set AGEAGE=AGE+1 where AGE>p_age;

end;

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

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 you can see from the above program, the application must also bypass the Hibernate API and call stored procedures directly through the JDBC API.

Thank you for reading! About "Hibernate how to achieve batch processing" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report