Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<1000000; i++ ) { Student student = new Student(.....); session.save(student); } tx.commit(); session.close();
Session session = SessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<1000000; i++ ) { Student student = new Student(.....); 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();
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); ScrollableResults studentCursor = session.createQuery("FROM STUDENT").scroll(); int count = 0; while(studentCursor .next()) { Student student = (Student) studentCursor.get(0); student.setName("DEV"); seession.update(student); if ( ++count % 50 == 0 ) { session.flush(); session.clear(); } } tx.commit(); session.close();
hibernate.jdbc.batch_size 50
hibernate.cache.use_second_level_cache false
<hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernateDB2</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.pool_size">1</property> <property name="hibernate.jdbc.batch_size"> 50 </property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="hibernate.cache.use_second_level_cache">false</property> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="com.sdnext.hibernate.tutorial.dto.Student"> </mapping></session-factory> </hibernate-configuration>Student.java
package com.sdnext.hibernate.tutorial.dto; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="STUDENT") public class Student implements Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 8633415090390966715L; @Id @Column(name="ID") @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(name="STUDENT_NAME") private String studentName; @Column(name="ROLL_NUMBER") private int rollNumber; @Column(name="COURSE") private String course; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String toString() { return "ROLL Number: "+rollNumber+"| Name: "+studentName+"| Course: "+course; } }HibernateTestDemo.java
package com.sdnext.hibernate.tutorial; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import com.sdnext.hibernate.tutorial.dto.Student; public class HibernateTestDemo { /** * @param args */ public static void main(String[] args) { SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { String studentName = "DINESH " + i; int rollNumber = 9 + i; String course = "MCA " + i; Student student = new Student(); student.setStudentName(studentName); student.setRollNumber(rollNumber); student.setCourse(course); session.save(student); if( i % 50 == 0 ) { session.flush(); session.clear(); } } transaction.commit(); session.close(); } }
Student student = new Student(); Address address = new Address(); student.setName("DINESH RAJPUT"); address.setCity("DELHI"); student.setAddress(address); session.save(student);The problem is Hibernate looks at each SQL statement and checks to see if it is the same statement as the previously executed statement. If they are and if it hasn't reached the batch_size it will batch those two statements together using JDBC2 batch. However, if your statements look like the example above, hibernate will see alternating insert statements and will flush an individual insert statement for each record processed. So 1 million new students would equal a total of 2 million insert statements in this case. This is extremely bad for performance.
Labels: Hibernate