Jsp Hibernate batch update and batch Delete processing code

Source: Internet
Author: User

The following program uses the Hibernate API to update the AGE fields of all records whose AGE is greater than zero in the MERs table in batches:
Tx = session. beginTransaction (); Iterator MERs = session. find ("from Customer c where c. age> 0 "). iterator (); while (MERS MERs. hasNext () {Customer customer Customer = (Customer) MERs. next (); customer. setAge (customer. getAge () + 1);} tx. commit (); session. close ();

If the CUSTOMERS table contains 10 thousand records older than zero, the find () method of the Session will load 10 thousand Customer objects to the memory. When the tx. commit () method is executed, the cache is cleared. Hibernate executes 10 thousand update statements for updating the MERs table:
Update MERs set AGE =? .... Where ID = I; update CUSTOMERS set AGE =? .... Where ID = j ;...... Update MERs set AGE =? .... Where ID = k;

The above batch update methods have two disadvantages:
(1) It occupies a large amount of memory. You must first load 10 thousand Customer objects to the memory and then update them one by one.
(2) The number of update statements executed is too large. Each update statement can only update one Customer object. Only 10 thousand update statements can be used to update 10 thousand Customer objects, which frequently accesses the database, will greatly reduce the performance of the application.
To quickly release the memory occupied by 10 thousand Customer objects, you can call the evict () method of the Session to immediately release each Customer object after updating it:
Tx = session. beginTransaction (); Iterator MERs = session. find ("from Customer c where c. age> 0 "). iterator (); while (MERS MERs. hasNext () {Customer customer Customer = (Customer) MERs. next (); customer. setAge (customer. getAge () + 1); session. flush (); session. evict (customer);} tx. commit (); session. close ();

After the age attribute of a Customer object is modified in the preceding program, the flush () method and evict () method of the Session are called immediately () the method enables Hibernate to immediately update the database based on the state changes of the Customer object, and then immediately execute the related update statement. The evict () method is used to clear the Customer object from the cache, so as to release the memory occupied by it in time.
However, the evict () method can only slightly improve the performance of batch operations. No matter whether the evict () method is used or not, Hibernate must execute 10 thousand update statements to update 10 thousand Customer objects, this is an important factor affecting the performance of batch operations. Assume that Hibernate can directly execute the following SQL statement:
Update MERs set AGE = AGE + 1 where AGE> 0;

Then, the above update statement can update the 10 thousand records in the MERs table. However, Hibernate does not directly provide an interface for executing such update statements. The application must bypass the Hibernate API and use the jdbc api to execute the SQL statement:
Tx = session. beginTransaction (); Connection con = session. connection (); PreparedStatement stmt = con. prepareStatement ("update MERs set AGE = AGE + 1" + "where AGE> 0" before using stmt.exe cuteUpdate (); tx. commit ();

The above program demonstrates the process of directly accessing the database through the jdbc api by bypassing the Hibernate API. The application obtains the database connection used by the Session through the connection () method of the Session, creates the PreparedStatement object through it, and executes the SQL statement. It is worth noting that the application still declares the Transaction boundary through the Hibernate Transaction interface.
If the underlying database (such as Oracle) supports stored procedures, you can also perform batch update through stored procedures. The storage process runs directly in the database, accelerating the speed. You can define a stored procedure named batchUpdateCustomer () in the Oracle database. The Code is as follows:
Create or replace procedure batchUpdateCustomer (p_age in number) asbeginupdate CUSTOMERS set AGE = AGE + 1 where AGE> p_age; end;

The above stored procedure has a parameter p_age, which indicates the customer's age. The application can call the stored procedure in the following way:
Tx = session. beginTransaction (); Connection con = session. connection (); String procedure = "{call batchUpdateCustomer (?) } "; CallableStatement cstmt = con. prepareCall (procedure); cstmt. setInt (); // you can specify 0cstmt.exe cuteUpdate (); tx. commit ();

The above Code shows that the application must bypass the Hibernate API and call the stored procedure directly through the jdbc api.
Each update () method in the reload form of a Session can only update one object at a time, while some reloads of the delete () method can use HQL statements as parameters. For example:
Session. delete ("from Customer c where c. age> 0 ");

If the CUSTOMERS table contains 10 thousand records older than zero, the above Code can delete 10 thousand records. However, the delete () method of the Session does not execute the following delete statement:
Delete from MERs where AGE> 0;

The delete () method of the Session first loads 10 thousand Customer objects to the memory using the following select statement:
Select * from MERs where AGE> 0;

Next, run the following 10 thousand delete statements to delete the Customer objects one by one:
Delete from MERs where ID = I; delete from CUSTOMERS where ID = j ;...... Delete from MERs where ID = k;

It can be seen that batch update and batch Delete via Hibernate API are not recommended. Using JDBC APIs to execute related SQL statements or call related stored procedures is the best method for batch update and deletion. Both methods have the following advantages:
(1) You do not need to load a large amount of data in the database into the memory, and then update or modify them one by one, so it does not consume a large amount of memory.
(2) update or delete a large volume of data in an SQL statement.

Related Article

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.