Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize batch processing in Hibernate

Shulou Source: shulou.com Published: 2022-06-02 08:02:30 11月13日 Update

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!

Tags: Data procedure storage processing performance database program memory actual update fastest transaction content parameter instance age application method method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker MySQL NVidia MariaDB