Hibernate Batch processing

1047

Bài viết được sự cho phép của tác giả Giang Phan

Trong bài này tôi sẽ giới thiệu với các bạn 1 phần quan trọng của Hibernate trong quá trình thao tác với cơ sở dữ liệu đó chính là batch processing hay còn gọi là xử lý hàng loạt.

Hibernate Batch Processing là gì?

Xem xét một tình huống khi chúng ta cần tải insert một số lượng lớn các bản ghi vào cơ sở dữ liệu bằng cách sử dụng Hibernate.

try (SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
Session session = sessionFactory.openSession();) {

session.beginTransaction();

final int numberOfRecords = 10000000;
for ( int i=0; i < numberOfRecords; i++ ) {
Tag tag = new Tag();
tag.setName("Hibernate Batch Processing");
session.persist(tag);
}

session.getTransaction().commit();
}
Khi chạy chương trình trên, chúng ta sẽ gặp một exception OutOfMemmeryException, vì Hibernate sẽ lưu tất cả 10000000 bản ghi vào bộ nhớ L1 Cache nhưng bộ nhớ cache không đủ.

Để giải quyết vấn đề này, chúng ta sẽ sử dụng tính năng batch processing trong Hibernate. Tính năng này cho phép chúng ta gửi yêu cầu thực thi một lượng lớn câu lệnh SQL một lần, thay vì yêu cầu thực thi từng câu lệnh SQL một.

Hibernate GenerationType

Chúng ta cần nắm về GenerationType trước khi tìm hiểu cách sử dụng Hibernate Batch Processing.

Như đã giới thiệu ở bài viết “Các Annotation của Hibernate“, chúng ta có 4 loại generated identifier strategies: AUTO, IDENTITY, SEQUENCE, TABLE.

  • AUTO : loại này sẽ sử dụng một trong 3 loại IDENTITY, SEQUENCE hoặc TABLE dựa trên yêu cầu tương thích của database.
  • IDENTITY : sẽ map với auto-incremented column.
  • SEQUENCE : được sử dụng để ủy quyền tạo id cho cơ sở dữ liệu.
  • TABLE : không có implement trực tiếp trong cơ sở dữ liệu quan hệ. TABLE không mở rộng (scale) khi tăng số lượng kết nối đến cơ sở dữ liệu. Hơn nữa, ngay cả đối với một kết nối cơ sở dữ liệu, thời gian phản hồi tạo định danh lớn hơn 10 lần so với khi sử dụng IDENTITY hoặc SEQUENCE. Chi tiết các bạn tham khảo thêm bài viết “Why you should never use the TABLE identifier generator with JPA and Hibernate“. Vì lý do performance và scalability, chúng ta nên xem xét 2 yếu tố này trước khi sử dụng.
  • Ngoài ra, chúng ta có thể sử dụng UUID : là một lớp của Java cho phép bạn tạo ra một chuỗi 36 ký tự ngẫu nhiên.

Với Oracle, PostgreSQL, Hibernate nó sẽ gọi một Sequence có tên Hibernate_Sequence để tạo ra một giá trị tăng dần để gán giá trị cho cột này. Với các DB khác chẳng hạn như MySQL, DB2, SQL Server, Sysbase cột có thể là kiểu IDENTITY và giá trị của nó có thể tự tăng.

IDENTITY generator sẽ disable Hibernate sử dụng JDBC batch insert. JDBC batch updates và delete sẽ không bị ảnh hưởng bởi IDENTITY generator.

Chỉ các câu lệnh INSERT không thể được tạo batch bởi vì:

  • Hibernate cố gắng trì hoãn Persistence Context flush SQL cho đến giây phút cuối cùng có thể. Chiến lược này gọi là “transactional write behind”. Vào thời điểm Persistence Context flush, các câu lệnh INSERT đã được thực thi để Hibernate biết id Entity nào được gán cho các Entity được lưu trữ.
  • Các thay đổi đã flush chỉ hiển thị cho transaction hiện tại. Cho đến khi trasaction hiện tại được commit, không có thay đổi nào được nhìn thấy bởi các transaction đồng thời khác.
  • IDENTITY cho phép một cột số nguyên / bigint được tự động tăng theo yêu cầu. Quá trình gia tăng xảy ra bên ngoài transaction đang chạy hiện tại, do đó, việc rollback có thể được thực hiện để loại bỏ các giá trị đã gán (khoảng cách giá trị có thể xảy ra). Quá trình tự tăng rất hiệu quả vì nó sử dụng cơ chế khóa bên trong cơ sở dữ liệu. Hạn chế duy nhất là chúng ta không thể biết giá trị mới được gán trước khi thực hiện câu lệnh INSERT. Hạn chế này đang cản trở cơ chế “transactional write behind” của Hibernate. Vì lý do này, Hibernates vô hiệu hóa hỗ trợ lô JDBC cho Entity sử dụng IDENTITY generator.
  • TABLE không tốt về performance. Do đó, sử dụng IDENTITY vẫn là lựa chọn tốt nhất trên MySQL. Nếu cần tính năng batch trên MySQL, chúng ta có thể kết hợp Hibernate sử dụng jOOQ.

Do đó, cách tốt nhất để sử dụng Hibernate Batch Processing trong Hibernate là sử dụng SEQUENCE generator hoặc UUID.

Enable Hibernate Batch processing

Trong bài này, tôi sẽ sử dụng PostgreSQL để sử dụng tính năng Hibernate Batch Processing.

-- Database: gp_batch_processing
-- DROP DATABASE gp_batch_processing;
CREATE DATABASE gp_batch_processing
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'C'
LC_CTYPE = 'C'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;

-- SEQUENCE: public.tag_id_seq
-- DROP SEQUENCE public.tag_id_seq;
CREATE SEQUENCE public.tag_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;

ALTER SEQUENCE public.tag_id_seq
OWNER TO postgres;

-- Table: public.tag
-- DROP TABLE public.tag;
CREATE TABLE public.tag
(
id bigint NOT NULL DEFAULT nextval('tag_id_seq'::regclass),
name character varying COLLATE pg_catalog."default",
CONSTRAINT tag_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE public.tag
OWNER to postgres;

Tạo maven project, mở file pom.xml thêm các denpendency sau:

<project xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>com.gpcoder</groupId>     <artifactId>HibernateBatchProcessingTutorial</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>jar</packaging>     <name>HibernateBatchProcessingTutorial</name>     <url>http://maven.apache.org</url>     <properties>         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>         <maven.compiler.source>1.8</maven.compiler.source>         <maven.compiler.target>1.8</maven.compiler.target>     </properties>     <dependencies>         <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->         <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <version>1.18.10</version>         </dependency>         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-core</artifactId>             <version>5.4.7.Final</version>         </dependency>         <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->         <dependency>             <groupId>postgresql</groupId>             <artifactId>postgresql</artifactId>             <version>9.1-901.jdbc4</version>         </dependency>         <!-- Log4j2 -->         <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->         <dependency>             <groupId>org.apache.logging.log4j</groupId>             <artifactId>log4j-api</artifactId>             <version>2.13.1</version>         </dependency>         <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->         <dependency>             <groupId>org.apache.logging.log4j</groupId>             <artifactId>log4j-core</artifactId>             <version>2.13.1</version>         </dependency>     </dependencies> </project>

Trong ví dụ, tôi thêm thư viện Log4j2 để log SQL và parameter được tạo bởi Hibernate. Tạo file log4j2.xml trong thư mục resource:

<?xml version="1.0" encoding="UTF-8"?> <Configuration>     <Properties>         <Property name="logFolder">logs</Property>     </Properties>     <Appenders>         <!-- Console Appender -->         <Console name="Console" target="SYSTEM_OUT">             <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />         </Console>         <!-- Hibernate File Appender -->         <File name="HibernateFile" fileName="${logFolder}/hibernate.log">             <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />         </File>         <!-- GPCoder File Appender -->         <File name="GPCoderAppFile" fileName="${logFolder}/gpcoderApp.log">             <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />         </File>     </Appenders>     <Loggers>         <!-- Log everything in hibernate -->         <Logger name="org.hibernate" level="info" additivity="false">             <AppenderRef ref="Console" />             <AppenderRef ref="HibernateFile" />         </Logger>         <!-- Log SQL statements -->         <Logger name="org.hibernate.SQL" level="debug" additivity="false">             <AppenderRef ref="Console" />             <AppenderRef ref="HibernateFile" />         </Logger>         <!-- Parameters which are bound to SQL statements (ie. WHERE clause) -->         <Logger name="org.hibernate.type.descriptor.sql" level="trace" additivity="false">             <AppenderRef ref="Console"/>             <AppenderRef ref="HibernateFile" />         </Logger>         <!-- Log GP Coder App statements -->         <Logger name="com.gpcoder" level="debug" additivity="false">             <AppenderRef ref="Console" />             <AppenderRef ref="GPCoderAppFile" />         </Logger>         <Root level="error">             <AppenderRef ref="Console" />             <AppenderRef ref="GPCoderAppFile" />         </Root>     </Loggers> </Configuration>

Tạo file hibernate.cfg.xml và cấu hình như sau:

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>     <session-factory>         <!-- Database setting -->         <property name="connection.username">postgres</property>         <property name="connection.password">1234567</property>         <property name="connection.driver_class">org.postgresql.Driver</property>         <property name="connection.url">jdbc:postgresql://localhost:5432/gp_batch_processing</property>         <!-- JDBC connection pool (use the built-in) -->         <property name="connection.pool_size">4</property>         <!-- SQL dialect -->         <property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>         <!-- Enable Hibernate's automatic session context management -->         <property name="current_session_context_class">thread</property>         <!-- Show all executed SQL to console -->         <property name="jdbc.batch_size">10</property>         <property name="jdbc.order_inserts">true</property>         <property name="jdbc.order_updates">true</property>         <property name="generate_statistics">true</property>         <mapping class="com.gpcoder.entities.Tag" />     </session-factory> </hibernate-configuration>

Lưu ý:

  • Chúng ta bắt buộc phải thêm thuộc tính hibernate.jdbc.batch_size để enable tính năng Batch processing của Hibernate. Giá trị này phải lớn hơn 0. Thông thường người ta thường gán giá trị trong khoảng 10-30.
  • generate_statistics : sử dụng để theo thống kê số lượng SQL được thực thi, batch processing.
  • order_inserts, order_updates : Nói với Hibernate rằng hãy order câu lệnh INSERT và UPDATE tương tự nhau để improve performance.

Tạo Entity class:

Tag.java

package com.gpcoder.entities; import lombok.Data; import javax.persistence.*; import java.io.Serializable; import java.util.Set; @Data @Entity @Table public class Tag implements Serializable {     @Id     @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tag_generator")     @SequenceGenerator(name="tag_generator", sequenceName = "tag_id_seq", allocationSize=1)     private Long id;     @Column     private String name; }

Hibernate Batch Insert

Mọi thứ cấu hình Hibrernate Batch Processing đã xong. Các bạn xem chương trình bên dưới để xem cách sử dụng Batch Insert với Hibernate.

package com.gpcoder; import com.gpcoder.entities.Tag; import com.gpcoder.utils.HibernateUtils; import lombok.extern.log4j.Log4j2; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.stat.Statistics; @Log4j2 public class HibernateBatchProcessingExample {     public static void main(String[] args) {         try (SessionFactory sessionFactory = HibernateUtils.getSessionFactory();              Session session = sessionFactory.openSession();) {             log.info("Statistics enabled = " + sessionFactory.getStatistics().isStatisticsEnabled());             session.beginTransaction();             final int numberOfRecords = 30;             final int batchSize = 10; // same as the JDBC batch size             for (int i = 1; i <= numberOfRecords; i++) {                 Tag tag = new Tag();                 tag.setName("Hibernate Batch Processing " + i);                 session.persist(tag);                 if (i % batchSize == 0 && i != numberOfRecords) {                     log.info("Flush a batch of INSERT & release memory: {} time(s)", (i / batchSize));                     session.flush();                     session.clear();                 }             }             log.info("Flush the last time at commit time");             session.getTransaction().commit();         }     } }

Lưu ý:

  • Chúng ta sẽ sử dụng các phương thức flush() và clear() của đối tượng Session để Hibernate tiếp tục bản ghi này vào cơ sở dữ liệu thay vì lưu trữ chúng trong bộ nhớ cache. Nhờ vậy, bộ nhớ được giải phóng và không gặp vấn đề OutOfMemmeryException.
  • batchSize: chúng ta nên gán nó bằng với giá trị batch_size chúng ta đã cấu hình trong hibernate.cf.xml để đạt được performance tốt nhất.
  • Chúng ta không cần flush() ở vòng lặp cuối cùng vì nó mặc định sẽ được thực thi khi lệnh commit được gọi.

Output chương trình:

2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {5.4.7.Final} 2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 2020-Apr-02 16:30:47 PM [main] WARN  org.hibernate.orm.connections.pooling - HHH10001002: Using Hibernate built-in connection pool (not for production use!) 2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/gp_batch_processing] 2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001001: Connection properties: {user=postgres, password=****} 2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001003: Autocommit mode: false 2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 4 (min=1) 2020-Apr-02 16:30:47 PM [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL94Dialect 2020-Apr-02 16:30:48 PM [main] INFO  com.gpcoder.HibernateBatchProcessingExample - Statistics enabled = true 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] INFO  com.gpcoder.HibernateBatchProcessingExample - Flush a batch of INSERT & release memory: 1 time(s) 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 1] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [2] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 2] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [3] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 3] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [4] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 4] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [5] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 5] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [6] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 6] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [7] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 7] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [8] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 8] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [9] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 9] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [10] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 10] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [11] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] INFO  com.gpcoder.HibernateBatchProcessingExample - Flush a batch of INSERT & release memory: 2 time(s) 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 11] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [12] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 12] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [13] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 13] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [14] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 14] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [15] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 15] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [16] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 16] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [17] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 17] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [18] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 18] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [19] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 19] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [20] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 20] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [21] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - select nextval ('tag_id_seq') 2020-Apr-02 16:30:48 PM [main] INFO  com.gpcoder.HibernateBatchProcessingExample - Flush the last time at commit time 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 21] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [22] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 22] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [23] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 23] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [24] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 24] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [25] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 25] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [26] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 26] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [27] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 27] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [28] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 28] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [29] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 29] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [30] 2020-Apr-02 16:30:48 PM [main] DEBUG org.hibernate.SQL - insert into Tag (name, id) values (?, ?) 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Processing 30] 2020-Apr-02 16:30:48 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [31] 2020-Apr-02 16:30:48 PM [main] INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener - Session Metrics {     22812 nanoseconds spent acquiring 1 JDBC connections;     21149 nanoseconds spent releasing 1 JDBC connections;     744689 nanoseconds spent preparing 33 JDBC statements;     4893934 nanoseconds spent executing 30 JDBC statements;     2470413 nanoseconds spent executing 3 JDBC batches;     0 nanoseconds spent performing 0 L2C puts;     0 nanoseconds spent performing 0 L2C hits;     0 nanoseconds spent performing 0 L2C misses;     32588030 nanoseconds spent executing 3 flushes (flushing a total of 30 entities and 0 collections);     0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) } 2020-Apr-02 16:30:48 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/gp_batch_processing] Process finished with exit code 0

Như bạn thấy, chúng ta Hibernate đã thực thi: 3 JDBC batches, 3 flushes, 30 entities.

Hibernate Batch Update (Session scroll)

Cũng giống như lúc INSERT, khi truy xuất, UPDATE dữ liệu hãy dùng các method  flush() và clear() để giải phóng 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 dữ liệu.

package com.gpcoder; import com.gpcoder.entities.Tag; import com.gpcoder.utils.HibernateUtils; import lombok.extern.log4j.Log4j2; import org.hibernate.*; @Log4j2 public class HibernateBatchUpdateExample {     public static void main(String[] args) {         try (SessionFactory sessionFactory = HibernateUtils.getSessionFactory();              Session session = sessionFactory.openSession();) {             log.info("Statistics enabled = " + sessionFactory.getStatistics().isStatisticsEnabled());             session.beginTransaction();             final int batchSize = 10; // same as the JDBC batch size             try (ScrollableResults scrollableResults = session                     .createQuery( "FROM Tag" )                     .setCacheMode( CacheMode.IGNORE )                     .scroll( ScrollMode.FORWARD_ONLY );             ) {                 int count = 1;                 while ( scrollableResults.next() ) {                     Tag tag = (Tag) scrollableResults.get( 0 );                     tag.setName("Hibernate Batch Update " + count);                     if ( count % batchSize == 0 && !scrollableResults.isLast()) {                         log.info("Flush a batch of UPDATE & release memory: {} time(s)", (count / batchSize));                         session.flush();                         session.clear();                     }                     count++;                 }                 log.info("Flush the last time at commit time");             }             session.getTransaction().commit();         }     } }

Output:

2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {5.4.7.Final} 2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 2020-Apr-02 16:58:01 PM [main] WARN  org.hibernate.orm.connections.pooling - HHH10001002: Using Hibernate built-in connection pool (not for production use!) 2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/gp_batch_processing] 2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001001: Connection properties: {user=postgres, password=****} 2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001003: Autocommit mode: false 2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 4 (min=1) 2020-Apr-02 16:58:01 PM [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL94Dialect 2020-Apr-02 16:58:02 PM [main] INFO  com.gpcoder.HibernateBatchUpdateExample - Statistics enabled = true 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - select tag0_.id as id1_1_, tag0_.name as name2_1_ from Tag tag0_ 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [2] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 0] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [3] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 1] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [4] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 2] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [5] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 3] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [6] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 4] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [7] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 5] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [8] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 6] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [9] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 7] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [10] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 8] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [11] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 9] 2020-Apr-02 16:58:02 PM [main] INFO  com.gpcoder.HibernateBatchUpdateExample - Flush a batch of UPDATE & release memory: 1 time(s) 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 1] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [2] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 2] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [3] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 3] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [4] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 4] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [5] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 5] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [6] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 6] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [7] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 7] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [8] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 8] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [9] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 9] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [10] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 10] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [11] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [12] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 10] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [13] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 11] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [14] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 12] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [15] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 13] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [16] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 14] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [17] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 15] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [18] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 16] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [19] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 17] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [20] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 18] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [21] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 19] 2020-Apr-02 16:58:02 PM [main] INFO  com.gpcoder.HibernateBatchUpdateExample - Flush a batch of UPDATE & release memory: 2 time(s) 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 11] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [12] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 12] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [13] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 13] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [14] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 14] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [15] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 15] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [16] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 16] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [17] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 17] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [18] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 18] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [19] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 19] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [20] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 20] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [21] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [22] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 20] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [23] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 21] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [24] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 22] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [25] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 23] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [26] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 24] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [27] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 25] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [28] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 26] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [29] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 27] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [30] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 28] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([id1_1_] : [BIGINT]) - [31] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([name2_1_] : [VARCHAR]) - [Hibernate Batch Processing 29] 2020-Apr-02 16:58:02 PM [main] INFO  com.gpcoder.HibernateBatchUpdateExample - Flush the last time at commit time 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 21] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [22] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 22] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [23] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 23] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [24] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 24] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [25] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 25] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [26] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 26] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [27] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 27] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [28] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 28] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [29] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 29] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [30] 2020-Apr-02 16:58:02 PM [main] DEBUG org.hibernate.SQL - update Tag set name=? where id=? 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [Hibernate Batch Update 30] 2020-Apr-02 16:58:02 PM [main] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [BIGINT] - [31] 2020-Apr-02 16:58:02 PM [main] INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener - Session Metrics {     22833 nanoseconds spent acquiring 1 JDBC connections;     22036 nanoseconds spent releasing 1 JDBC connections;     218128 nanoseconds spent preparing 4 JDBC statements;     685155 nanoseconds spent executing 1 JDBC statements;     4171650 nanoseconds spent executing 3 JDBC batches;     0 nanoseconds spent performing 0 L2C puts;     0 nanoseconds spent performing 0 L2C hits;     0 nanoseconds spent performing 0 L2C misses;     44981036 nanoseconds spent executing 3 flushes (flushing a total of 30 entities and 0 collections);     37173 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections) } 2020-Apr-02 16:58:02 PM [main] INFO  org.hibernate.orm.connections.pooling - HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/gp_batch_processing] Process finished with exit code 0

Stateless Session

Trái với Session, StatelessSession sẽ không cache lại ở bất cứ mức nào: first-level cache, second-level hay query cache. Nó cũng không gắn với một Persistence Context nào. Vì vậy sử dụng cách này sẽ không sợ cache làm OutOfMemory.

Chi tiết các bạn tham khảo thêm Hibernate StatelessSession document.

Hibernate Query Language for DML

DML (Data Manipulation Language – Ngôn ngữ thao tác dữ liệu) đề cập đến các câu lệnh SQL như INSERT, UPDATE và DELETE. Hibernate cung cấp các phương thức để thực thi số lượng lớn câu lệnh DML dưới dạng Ngôn ngữ truy vấn Hibernate (HQL). Bằng cách này chúng ta sẽ gọi trực tiếp vào database nên không gặp vấn đề OutOfMemory.

Chi tiết các bạn tham khảo thêm Hibernate Query Language for DML document.

Tài liệu tham khảo:

Bài viết gốc được đăng tải tại gpcoder.com

Có thể bạn quan tâm:

Xem thêm Việc làm Developer hấp dẫn trên TopDev