鍍金池/ 教程/ Java/ 批處理
例子
注釋
映射類型
ORM 概覽
環(huán)境
標(biāo)準(zhǔn)查詢
原生 SQL
持久化類
查詢語言
配置
批處理
緩存
架構(gòu)
會(huì)話
映射文件
O/R 映射
簡(jiǎn)介
攔截器

批處理

考慮一種情況,你需要使用 Hibernate 將大量的數(shù)據(jù)上傳到你的數(shù)據(jù)庫中。以下是使用 Hibernate 來達(dá)到這個(gè)的代碼片段:

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Employee employee = new Employee(.....);
    session.save(employee);
}
tx.commit();
session.close();

因?yàn)槟J(rèn)下,Hibernate 將緩存所有的在會(huì)話層緩存中的持久的對(duì)象并且最終你的應(yīng)用程序?qū)⒑?OutOfMemoryException 在第 50000 行的某處相遇。你可以解決這個(gè)問題,如果你在 Hibernate 使用批處理。

為了使用批處理這個(gè)特性,首先設(shè)置 hibernate.jdbc.batch_size 作為批處理的尺寸,取一個(gè)依賴于對(duì)象尺寸的值 20 或 50。這將告訴 hibernate 容器每 X 行為一批插入。為了在你的代碼中實(shí)現(xiàn)這個(gè)我們將需要像以下這樣做一些修改:

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

上面的代碼將使 INSERT 操作良好運(yùn)行,但是如果你愿意進(jìn)行 UPDATE 操作那么你可以使用以下代碼達(dá)到這一點(diǎn):

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

ScrollableResults employeeCursor = session.createQuery("FROM EMPLOYEE")
                                   .scroll();
int count = 0;

while ( employeeCursor.next() ) {
   Employee employee = (Employee) employeeCursor.get(0);
   employee.updateEmployee();
   seession.update(employee); 
   if ( ++count % 50 == 0 ) {
      session.flush();
      session.clear();
   }
}
tx.commit();
session.close();

批處理樣例

讓我們通過添加 hibernate.jdbc.batch_size 屬性來修改配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume students is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/test
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      root123
   </property>
   <property name="hibernate.jdbc.batch_size">
      50
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>

考慮以下 POJO Employee 類:

public class Employee {
   private int id;
   private String firstName; 
   private String lastName;   
   private int salary;  

   public Employee() {}
   public Employee(String fname, String lname, int salary) {
      this.firstName = fname;
      this.lastName = lname;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName( String first_name ) {
      this.firstName = first_name;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName( String last_name ) {
      this.lastName = last_name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

讓我們創(chuàng)建以下的 EMPLOYEE 表單來存儲(chǔ) Employee 對(duì)象:

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

以下是將 Employee 對(duì)象和 EMPLOYEE 表單配對(duì)的映射文件。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

最后,我們將用 main() 方法來創(chuàng)建我們的應(yīng)用程序類以運(yùn)行應(yīng)用程序,我們將使用 Session 對(duì)象和可用的 flush()clear() 方法以讓 Hibernate 持續(xù)將這些記錄寫入數(shù)據(jù)庫而不是在內(nèi)存中緩存它們。

import java.util.*; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageEmployee {
   private static SessionFactory factory; 
   public static void main(String[] args) {
      try{
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }
      ManageEmployee ME = new ManageEmployee();

      /* Add employee records in batches */
      ME.addEmployees( );
   }
   /* Method to create employee records in batches */
   public void addEmployees( ){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer employeeID = null;
      try{
         tx = session.beginTransaction();
         for ( int i=0; i<100000; i++ ) {
            String fname = "First Name " + i;
            String lname = "Last Name " + i;
            Integer salary = i;
            Employee employee = new Employee(fname, lname, salary);
            session.save(employee);
            if( i % 50 == 0 ) {
               session.flush();
               session.clear();
            }
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return ;
   }
}

編譯和執(zhí)行

這里是編譯和運(yùn)行以上提及的應(yīng)用程序的步驟。確保你已經(jīng)在處理編譯和運(yùn)行前已經(jīng)正確設(shè)置了 PATH 和 CLASSPATH。

  • 如上面解釋的那樣創(chuàng)建 hibernate.cfg.xml 配置文件。
  • 如上面顯示的那樣創(chuàng)建 Employee.hbm.xml 映射文件。
  • 如上面顯示的那樣創(chuàng)建 Employee.java 源文件并編譯。
  • 如上面顯示的那樣創(chuàng)建 ManageEmployee.java 源文件并編譯。
  • 執(zhí)行 ManageEmployee 二進(jìn)制代碼來運(yùn)行可以在 EMPLOYEE 表單中創(chuàng)建 100000 個(gè)記錄的程序。
上一篇:ORM 概覽