Hibernate Reading Notes ----- batch processing of hibernate

Source: Internet
Author: User

Hibernate operates the database in an object-oriented way. When the persistence object is operated in an object-oriented way in the program, it is automatically converted to database operations. However, if we want to update 100000 records at the same time, do we need to load 100000 records one by one, and then call the setter method in sequence. This method is too cumbersome and seriously affects the data access performance. Hibernate provides a batch processing solution to solve this problem.

1. Batch insert
If we need to insert 100000 records, we may use the following method through hibernate:
[Java]
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();
For (int I = 0; I <100000; I ++ ){
User user = new User ();
User. setName ("userName" + I );
User. setAge (I );
Session. save (session );
}
Tx. commit ();
Session. close ();
However, this program has a problem: when the program runs somewhere, it will always throw an OutOfMemoryException memory overflow exception. This is because the Hibernate Session holds a required level-1 cache, and all User instances cache sessions.
If you want to perform batch processing and achieve an ideal performance, it is vital to use the JDBC batching function. Set the batch size parameter of JDBC to an appropriate value.
[Html] view plaincopyprint?
<Property name = "jdbc. batch_size"> 20 </property>
You may also want to disable the second-level cache:
[Html] view plaincopyprint?
Hibernate. cache. use_second_level_cache false
But this is not necessary.
The solution to this problem is to regularly fl session cache data into the database, and control the size of a level-1 cache by calling clear. As follows:
[Java]
Static void addUser () throws Exception {
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();
// Insert 1000 records for 1000 cycles
For (int I = 0; I <100000; I ++ ){
// Create a User object
User user = new User ();
User. setName ("userName" + I );
User. setAge (I );

// Cache the User instance at the session level
Session. save (user );

// When the accumulators are multiples of 20, the session data is flushed into the database, and the session is cached.
If (I % 20 = 0 ){
Session. flush ();
Session. clear ();
}
}
 
<Span style = "font-size: 16px;"> </span>
 
Ii. Batch update
The method described above also applies to batch update. You need to use the scroll () method to take full advantage of the benefits of server-side cursors when returning a lot of row data for queries.
[Java]
Static void updateUser () throws Exception {
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();
// Query all records in the User table
ScrollableResults re = session. createQuery ("from User"). setCacheMode (CacheMode. IGNORE). scroll (ScrollMode. FORWARD_ONLY );
Int count = 1;
While (re. next ()){
User user = (User) re. get (0 );
// Flush the update result from the session to the database when count = 20
User. setName ("New user:" + count );
If (++ count % 20 = 0 ){
Session. flush ();
Session. clear ();
}
}
Tx. commit ();
Session. close ();
}

Although batch update can be performed in this way, the execution efficiency is not high because it needs to execute Data Query before data update can be executed. To avoid this situation, we can use the DML style for batch data updates.
 
3. DML-style batch update/Deletion
Hibernate provides a way to use the Hibernate query language (HQL) to execute a large number of SQL-style DML statements.
The syntax format of batch update and delete statements is as follows: (UPDATE | DELETE) FROM? EntityName (WHERE where_conditions ).
Note the following points:
1. In the FROM clause, the FROM keyword is optional.
2. The FROM clause (from-clause) can have only one entity name, which can be an alias. If the object name is an alias, any referenced attribute must be prefixed with this alias. If it is not an alias, any attribute reference with a prefix is invalid.
3. Connections cannot be used in batch HQL statements, either explicitly or implicitly. However, subqueries can be used in the WHERE clause. Subqueries can be used in the where clause. subqueries can contain joins.
4. The entire WHERE clause is optional.
Instance: Use query.exe cuteUpdate () to execute an hql update statement.
[Java]
Static void updateUser () throws Exception {
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();

// Define the HQL statement www.2cto.com for batch update
String hql = "update User as user set user. name =: name ";
// Execute update
Session. createQuery (hql). setString ("name", "name" cmd.exe cuteUpdate ();

Tx. commit ();
Session. close ();
}

When using this batch update syntax, you usually only need to execute an SQL update statement to complete all updates that meet the conditions. However, you may also need to execute multiple update statements because of inheritance ing.
Execute an hql delete statement and use the Query.exe cuteUpdate () method as follows:
[Java]
// Batch Delete using the DML Style
Static void deleteUser () throws Exception {
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();

// Define HQL statements for batch update
String hql = "delete User ";

// Execute Delete
Session.createquery(hqlw..exe cuteUpdate ();

Tx. commit ();
Session. close ();
}

The Query.exe cuteUpdate () method returns an integer value, which is the number of records affected by this operation.

Author: chenssy

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.