Data batch processing solution in Hibernate

Source: Internet
Author: User
Tags commit flush
Many people are skeptical about whether Java is suitable for batch data processing. As a result, it will be deemed that ORM may not be particularly suitable for batch data processing.
In fact, I think if we use it properly, we can eliminate the performance concerns of ORM in batch processing. The following uses Hibernate as an example to illustrate how to use Hibernate in Java to batch process data.
Insert 100 pieces of data into the database. Hibernate may be like this:
Session session = sessionFactory. openSession ();
Transaction tx = session. beginTransaction ();
For (int I = 0; I <100000; I ++ ){
Customer customer = new Customer (.....);
Session. save (customer );}
Tx. commit ();
Session. close ();
Memory overflow may occur when the number of logs reaches 50th. This is why Hibernate caches all recently inserted customers in the memory with session-level cache. Do not forget that Hiberante does not limit the cache size of the first-level cache:
# The Persistent Object instance is managed at the end of the transaction. At this time, Hibernate synchronizes with the database any managed objects that have been changed.
# Session implements asynchronous write-behind, which allows Hibernate to explicitly write a batch of operations.
Here, I will show how Hibernate implements batch insert:
First, we set a reasonable JDBC batch size, hibernate. jdbc. batch_size 20.
Then, flush () and clear () are performed on the Session at a certain interval ().
Session session = sessionFactory. openSession ();
Transaction tx = session. beginTransaction ();
For (int I = 0; I <100000; I ++ ){
Customer customer = new Customer (.....);
Session. save (customer );
If (I % 20 = 0 ){
// Flush insert data and release memory:
Session. flush (); session. clear ();}
}
Tx. commit ();
Session. close ();
So how to delete and update data? Well, in Hibernate2.1.6 or a later version, scroll () will be the best way:
Session session = sessionFactory. openSession ();
Transaction tx = session. beginTransaction ();
ScrollableResults MERS = session. getNamedQuery ("GetCustomers ")
. Scroll (ScrollMode. FORWARD_ONLY );
Int count = 0;
While (customers. next ()){
Customer customer = (Customer) MERS mers. get (0 );

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.