package com.gmail.newmanhuang.learnhibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import com.gmail.newmanhuang.learnhibernate.model.Person;
import java.sql.*;
public class LearnHibernateMain {
private Configuration config;
private SessionFactory sessionFactory;
private Session session;
public static void main(String[] args) {
LearnHibernateMain lh=new LearnHibernateMain();
//
用
hibernate
创建
10000
条记录,分
10
次插入,每次
1000
条,每
100
条记录做一次批量插入
//lh.createPersons(10, 1000, 100);
//
用
jdbc
直接创建
10000
条记录,分
10
次插入,每次
1000
条,每
100
条记录做一次批量插入
lh.createPersonsByJDBC(10, 1000,100);
}
//
用
hibernate
创建
person
记录
, loopNum
为循环插入的次数,
batchNum1
为每次循环插入的记录数,
batchNum2
为物理批量插入记录数
private void createPersons(int loopNum,int batchNum1,int batchNum2){
setup();
System.out.println("hibernate record creating testing.\r\n"
+ "loop times:" + loopNum + "\r\nbatch number:" + batchNum1);
for(int i=0;i<loopNum;i++){
try {
Thread.sleep(50);//
休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
long fPoint=System.currentTimeMillis();
Transaction tx = session.beginTransaction();
for(int j=0;j<batchNum1;j++){
Person person = new Person();
person.setName("name-" + i +"-"+ j);
person.setAge(new Integer(25));
session.save(person);
//batch flush
if ( j % batchNum2 == 0 ) {//
执行物理批量插入
session.flush();
session.clear();
}
}
tx.commit();
long tPoint=System.currentTimeMillis();
//
打印插入
batchNum1
条记录的速率
(
条
/
秒
)
System.out.println(
"the " + i + " batch" + "(" + batchNum1 +") rcds/s:"
+ ((double)batchNum1/(tPoint-fPoint))*1000);
}
teardown();
}
//
用
jdbc
创建
person
记录
, loopNum
为循环插入的次数,
batchNum1
为每次循环插入的记录数,
batchNum2
为物理批量插入记录数
private void createPersonsByJDBC(int loopNum,int batchNum1,int batchNum2){
System.out.println("JDBC record creating testing.\r\n"
+ "loop times:" + loopNum + "\r\nbatch number:" + batchNum1);
Connection conn=getDBConn();
try{
PreparedStatement pstmt=conn.prepareStatement("insert into person(name,age) values(?,?)");
for(int i=0;i<loopNum;i++){
try {
Thread.sleep(50);//
休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
long fPoint=System.currentTimeMillis();
conn.setAutoCommit(false);
for(int j=0;j<batchNum1;j++){
String name="name-" + i +"-"+ j;
pstmt.setString(1, name);
pstmt.setInt(2, 25);
pstmt.addBatch();
if(j%batchNum2==0){//
执行物理批量插入
pstmt.executeBatch();
conn.commit();
}
}
pstmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
long tPoint=System.currentTimeMillis();
//
打印插入
batchNum1
条记录的速率
(
条
/
秒
)
System.out.println(
"the " + i + " batch" + "(" + batchNum1 +") rcds/s:"
+ ((double)batchNum1/(tPoint-fPoint))*1000);
}
pstmt.close();
}catch(Exception x){
try{
conn.close();
}catch(Exception x1){
}
}
}
//
获取
JDBC
连接
private Connection getDBConn(){
Connection conn=null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/learnhibernate", "root", "");
} catch (Exception x) {
}
return conn;
}
//
初始化
hibernate
数据库环境
private void setup(){
config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
}
//
销毁
hibernate
数据库环境
private void teardown(){
session.close();
sessionFactory.close();
}
}
|