批处理

    因为默认下,Hibernate 将缓存所有的在会话层缓存中的持久的对象并且最终你的应用程序将和 OutOfMemoryException 在第 50000 行的某处相遇。你可以解决这个问题,如果你在 Hibernate 使用批处理

    为了使用批处理这个特性,首先设置 hibernate.jdbc.batch_size 作为批处理的尺寸,取一个依赖于对象尺寸的值 20 或 50。这将告诉 hibernate 容器每 X 行为一批插入。为了在你的代码中实现这个我们将需要像以下这样做一些修改:

    1. Transaction tx = session.beginTransaction();
    2. for ( int i=0; i<100000; i++ ) {
    3. Employee employee = new Employee(.....);
    4. session.save(employee);
    5. if( i % 50 == 0 ) { // Same as the JDBC batch size
    6. //flush a batch of inserts and release memory:
    7. session.flush();
    8. session.clear();
    9. }
    10. }
    11. tx.commit();
    12. session.close();

    让我们通过添加 hibernate.jdbc.batch_size 属性来修改配置文件:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <!DOCTYPE hibernate-configuration SYSTEM
    3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    4. <hibernate-configuration>
    5. <session-factory>
    6. <property name="hibernate.dialect">
    7. org.hibernate.dialect.MySQLDialect
    8. </property>
    9. <property name="hibernate.connection.driver_class">
    10. com.mysql.jdbc.Driver
    11. </property>
    12. <!-- Assume students is the database name -->
    13. <property name="hibernate.connection.url">
    14. jdbc:mysql://localhost/test
    15. </property>
    16. <property name="hibernate.connection.username">
    17. root
    18. </property>
    19. </property>
    20. <property name="hibernate.jdbc.batch_size">
    21. 50
    22. </property>
    23. <!-- List of XML mapping files -->
    24. <mapping resource="Employee.hbm.xml"/>
    25. </session-factory>
    26. </hibernate-configuration>

    考虑以下 POJO Employee 类:

    1. create table EMPLOYEE (
    2. id INT NOT NULL auto_increment,
    3. first_name VARCHAR(20) default NULL,
    4. last_name VARCHAR(20) default NULL,
    5. salary INT default NULL,
    6. PRIMARY KEY (id)
    7. );

    以下是将 Employee 对象和 EMPLOYEE 表单配对的映射文件。

    最后,我们将用 main() 方法来创建我们的应用程序类以运行应用程序,我们将使用 Session 对象和可用的 flush()clear() 方法以让 Hibernate 持续将这些记录写入数据库而不是在内存中缓存它们。

    1. import java.util.*;
    2. import org.hibernate.HibernateException;
    3. import org.hibernate.Session;
    4. import org.hibernate.Transaction;
    5. import org.hibernate.SessionFactory;
    6. import org.hibernate.cfg.Configuration;
    7. public class ManageEmployee {
    8. private static SessionFactory factory;
    9. public static void main(String[] args) {
    10. try{
    11. factory = new Configuration().configure().buildSessionFactory();
    12. }catch (Throwable ex) {
    13. System.err.println("Failed to create sessionFactory object." + ex);
    14. throw new ExceptionInInitializerError(ex);
    15. /* Add employee records in batches */
    16. ME.addEmployees( );
    17. }
    18. /* Method to create employee records in batches */
    19. public void addEmployees( ){
    20. Session session = factory.openSession();
    21. Transaction tx = null;
    22. Integer employeeID = null;
    23. try{
    24. tx = session.beginTransaction();
    25. for ( int i=0; i<100000; i++ ) {
    26. String fname = "First Name " + i;
    27. String lname = "Last Name " + i;
    28. Integer salary = i;
    29. Employee employee = new Employee(fname, lname, salary);
    30. session.save(employee);
    31. if( i % 50 == 0 ) {
    32. session.flush();
    33. session.clear();
    34. }
    35. }
    36. tx.commit();
    37. }catch (HibernateException e) {
    38. if (tx!=null) tx.rollback();
    39. e.printStackTrace();
    40. }finally {
    41. session.close();
    42. }
    43. return ;
    44. }
    45. }

    编译和执行

    • 如上面解释的那样创建 hibernate.cfg.xml 配置文件。
    • 如上面显示的那样创建 Employee.hbm.xml 映射文件。
    • 如上面显示的那样创建 Employee.java 源文件并编译。
    • 执行 ManageEmployee 二进制代码来运行可以在 EMPLOYEE 表单中创建 100000 个记录的程序。