Hibernate Batch Processing là gì? Batch Processing trong Hibernate

Hibernate Batch Processing là gì? Batch Processing trong Hibernate

1. Hibernate Batch Processing là gì?

Ta có tình huống như sau: cần insert 10000 bản ghi vào database

  • Nếu insert lần lượt và đẩy từng đối tượng một thì thời gian sẽ rất lâu vì phải mở/đóng connection nhiều lần
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    session.save(customer);
    session.flush();
    session.clear();
}
tx.commit();
session.close();
  • Nếu insert và đẩy đồng thời cùng 1 lúc 10000 bản ghi thì sẽ xảy ra lỗi OutOfMemoryException (Lý do là hibernate sẽ lưu tất cả 10000 bản ghi vào bộ nhớ cache nhưng bộ nhớ cache không đủ)
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();

=> Giải pháp: Batch Processing

Vậy Batch Processing là gi? Batch Processing là xử lý theo lô, tức là ta sẽ insert và đẩy từng lô bản ghi vào database.

Hibernate Batch Processing là gì? Batch Processing trong Hibernate

Ví dụ ở đây ta sẽ insert và đẩy 50 bản ghi vào database cùng 1 lúc, như thế sẽ giảm số lượng connection tới database, đồng thời tránh được lỗi OutOfMemoryException

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
   
for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    session.save(customer);
    if ( i % 50 == 0 ) { //50, same as the JDBC batch size
        //flush a batch of inserts and release memory:
        session.flush();
        session.clear();
    }
}
   
tx.commit();
session.close();

2. Batch Inserts

Khi bạn tạo mới đối tượng persistent, sử dụng các method flush() và clear() để điều khiển kích thương của bộ nhớ cache.

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 ) { //20, same as the JDBC batch size
        //flush a batch of inserts and release memory:
        session.flush();
        session.clear();
    }
}
   
tx.commit();
session.close();

3. Batch Update

Cũng giống như lúc insert, khi truy xuất, cập nhật dữ liệu hãy dùng các method  flush() và clear() để điều khiển kích thương của bộ nhớ cache. Ngoài ra sử dụng method scroll() để tận dụng các con trỏ ở server cho các truy vấn trả về nhiều hàng dữ liệu.

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
   
ScrollableResults customers = session.getNamedQuery("GetCustomers")
    .setCacheMode(CacheMode.IGNORE)
    .scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( customers.next() ) {
    Customer customer = (Customer) customers.get(0);
    customer.updateStuff(...);
    if ( ++count % 20 == 0 ) {
        //flush a batch of updates and release memory:
        session.flush();
        session.clear();
    }
}
   
tx.commit();
session.close();

4. StatelessSession

Trái với Session, StatelessSession không có cache-level 1 hoặc giao tiếp với cache-level 2. Nó cũng không gắn với một Persistence Context nào.

StatelessSession session = factory.openStatelessSession();
Transaction tx = session.beginTransaction();
   
for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    session.insert(customer);
}
   
tx.commit();
session.close();

 

References:

https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch04.html

stackjava.com