hibernate效率问题为什么我加了批处理1000多条数据还要1分钟才能插入完成,大家帮我看下代码看看有什么可以优化的
实体类
package com.probiz.estorepf.entity;
import java.util.HashSet;
import java.util.Set;
/**
* Tclass entity. @author MyEclipse Persistence Tools
*/
public class Tclass implements java.io.Serializable {
// Fields
private Long cid;
private String cname;
private Set students = new HashSet(0);
// Constructors
/** default constructor */
public Tclass() {
}
/** minimal constructor */
public Tclass(String cname) {
this.cname = cname;
}
/** full constructor */
public Tclass(String cname, Set students) {
this.cname = cname;
this.students = students;
}
// Property accessors
public Long getCid() {
return this.cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Set getStudents() {
return this.students;
}
public void setStudents(Set students) {
this.students = students;
}
}
hibernate配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<!-- 配置JDBC里batch的大小 -->
<property name="jdbc.batch_size">50</property>
<!-- 推荐关闭Hibernate的二级缓存以提高效率 -->
<property name="cache.use_second_level_cache">false</property>
<!-- 配置线程安全的session -->
<property name="current_session_context_class">thread</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
</property>
<!-- 配置连接池 -->
<property name="c3p0.max_size">20</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">5000</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.idle_test_period">3000</property>
<property name="c3p0.acquire_increment">20</property>
<property name="c3p0.validate">false</property>
<property name="myeclipse.connection.profile">test</property>
<mapping resource="com/probiz/estorepf/entity/Student.hbm.xml" />
<mapping resource="com/probiz/estorepf/entity/Foo.hbm.xml" />
<mapping resource="com/probiz/estorepf/entity/Tclass.hbm.xml" />
<mapping resource="com/probiz/estorepf/entity/Bar.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.probiz.estorepf.entity.Foo;
import com.probiz.estorepf.entity.Student;
import com.probiz.estorepf.entity.Tclass;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//testSave();
// testDeleteTclass();
// testUpdateClass();
// Foo foo1 = new Foo("foo1");
// Session session = HibernateSessionFactory.getSession();
// Tclass c = (Tclass) session.load(Tclass.class, Long.valueOf(1L));
// session.save(foo1);
// session.beginTransaction().commit();
// session.close();
testSave();
}
public static void testSave() {
// Student s1 = new Student();
// Student s2 = new Student();
// s1.setSname("张三");
// s1.setTclass(c);
// s2.setSname("李四");
// s2.setTclass(c);
// c.getStudents().add(s1);
// c.getStudents().add(s2);
Session session = HibernateSessionFactory.getSession();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(new Date()));
for (int i = 0; i <1000; i++) {
Tclass c = new Tclass();
c.setCname("4579");
Transaction tx = session.beginTransaction();
session.save(c);
System.out.println(i);
if(i%50==0) //以每50个数据作为一个处理单元
{
session.flush(); //保持与数据库数据的同步
session.clear(); //清除内部缓存的全部数据,及时释放出占用的内存
}
tx.commit();
}
System.out.println(formatter.format(new Date()));
session.close();
}
}