1、导入c3p0的jar包
hibernate-release-4.3.11.Final/lib/optional/c3p0/*.jar
2、加入配置
- hibernate.c3p0.max_size: 数据库连接池的最大连接数
- hibernate.c3p0.min_size: 数据库连接池的最小连接数
- hibernate.c3p0.timeout: 数据库连接池中连接对象在多长时间没有使用过后,就应该被销毁
- hibernate.c3p0.max_statements: 缓存 Statement 对象的数量
- hibernate.c3p0.idle_test_period: 表示连接池检测线程多长时间检测一次池内的所有链接对象是否超时. 连接池本身不会把自己从连接池中移除,而是专门有一个线程按照一定的时间间隔来做这件事,这个线程通过比较连接对象最后一次被使用时间和当前时间的时间差来和 timeout 做对比,进而决定是否销毁这个连接对象。
- hibernate.c3p0.acquire_increment: 当数据库连接池中的连接耗尽时, 同一时刻获取多少个数据库连接
<property name&#61;"hibernate.c3p0.max_size">10property>
<property name&#61;"hibernate.c3p0.min_size">5property>
<property name&#61;"hibernate.c3p0.acquire_increment">2property>
<property name&#61;"hibernate.c3p0.idle_test_period">2000property>
<property name&#61;"hibernate.c3p0.timeout">2000property>
<property name&#61;"hibernate.c3p0.max_statements">2property>
&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
- hibernate.jdbc.fetch_size&#xff1a;实质是调用
Statement.setFetchSize()
方法设定 JDBC 的 Statement 读取数据的时候每次从数据库中取出的记录条数
。例如一次查询1万条记录&#xff0c;对于Oracle的JDBC驱动来说&#xff0c;是不会 1 次性把1万条取出来的&#xff0c;而只会取出 fetchSize 条数&#xff0c;当结果集遍历完了这些记录以后&#xff0c;再去数据库取 fetchSize 条数据。因此大大节省了无谓的内存消耗。Fetch Size设的越大&#xff0c;读数据库的次数越少&#xff0c;速度越快&#xff1b;Fetch Size越小&#xff0c;读数据库的次数越多&#xff0c;速度越慢。Oracle数据库的JDBC驱动默认的Fetch Size &#61; 10&#xff0c;是一个保守的设定&#xff0c;根据测试&#xff0c;当Fetch Size&#61;50时&#xff0c;性能会提升1倍之多&#xff0c;当 fetchSize&#61;100&#xff0c;性能还能继续提升20%&#xff0c;Fetch Size继续增大&#xff0c;性能提升的就不显著了。并不是所有的数据库都支持Fetch Size特性&#xff0c;例如MySQL就不支持 - hibernate.jdbc.batch_size&#xff1a;
设定对数据库进行批量删除&#xff0c;批量更新和批量插入的时候的批次大小
&#xff0c;类似于设置缓冲区大小的意思。batchSize 越大&#xff0c;批量操作时向数据库发送sql的次数越少&#xff0c;速度就越快。
测试结果是当Batch Size&#61;0的时候&#xff0c;使用Hibernate对Oracle数据库删除1万条记录需要25秒&#xff0c;Batch Size &#61; 50的时候&#xff0c;删除仅仅需要5秒&#xff01;Oracle数据库 batchSize&#61;30 的时候比较合适。
<property name&#61;"hibernate.jdbc.fetch_size">100property>
<property name&#61;"hibernate.jdbc.batch_size">50property>
代码测试&#xff1a;
hibernate.cfg.xml&#xff1a;
<hibernate-configuration>
<session-factory><property name&#61;"hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty><property name&#61;"hibernate.connection.url">jdbc:mysql:///hibernate_dbproperty><property name&#61;"hibernate.connection.username">rootproperty><property name&#61;"hibernate.connection.password">rootproperty><property name&#61;"hibernate.show_sql">trueproperty><property name&#61;"hibernare.format_sql">trueproperty><property name&#61;"hibernate.hbm2ddl.auto">updateproperty><property name&#61;"hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty><property name&#61;"hibernate.use_identifier_rollback">trueproperty><property name&#61;"hibernate.c3p0.max_size">10property><property name&#61;"hibernate.c3p0.min_size">5property><property name&#61;"hibernate.c3p0.acquire_increment">2property><property name&#61;"hibernate.c3p0.idle_test_period">2000property><property name&#61;"hibernate.c3p0.timeout">2000property><property name&#61;"hibernate.c3p0.max_statements">2property><property name&#61;"hibernate.jdbc.fetch_size">100property><property name&#61;"hibernate.jdbc.batch_size">10property><mapping resource&#61;"com/zm/c3p0/news.hbm.xml"/>
session-factory>
hibernate-configuration>
news.hbm.xml&#xff1a;
<hibernate-mapping><class name&#61;"com.zm.c3p0.News" table&#61;"news" schema&#61;"hibernate_db" select-before-update&#61;"true"><id name&#61;"id" type&#61;"java.lang.Integer"><column name&#61;"id" sql-type&#61;"int(11)"/><generator class&#61;"native">generator>id><property name&#61;"title" type&#61;"java.lang.String"><column name&#61;"title" sql-type&#61;"varchar(255)"/>property><property name&#61;"auth" type&#61;"java.lang.String"><column name&#61;"auth" sql-type&#61;"varchar(255)"/>property><property name&#61;"desc" type&#61;"java.lang.String"><column name&#61;"desc" sql-type&#61;"varchar(255)"/>property>class>
hibernate-mapping>
News&#xff1a;
public class News {private int id;private String title;private String auth;private String desc;public News() {}public News(String title, String auth) {this.id &#61; id;this.title &#61; title;this.auth &#61; auth;}public int getId() {return id;}public void setId(int id) {this.id &#61; id;}public String getTitle() {return title;}public void setTitle(String title) {this.title &#61; title;}public String getAuth() {return auth;}public void setAuth(String auth) {this.auth &#61; auth;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc &#61; desc;}&#64;Overridepublic String toString() {return "News{" &#43;"id&#61;" &#43; id &#43;", title&#61;&#39;" &#43; title &#43; &#39;\&#39;&#39; &#43;", auth&#61;&#39;" &#43; auth &#43; &#39;\&#39;&#39; &#43;", desc&#61;&#39;" &#43; desc &#43; &#39;\&#39;&#39; &#43;&#39;}&#39;;}
}
测试&#xff1a;
public class HibernateC3p0Test {private SessionFactory sessionFactory;private Session session;private Transaction transaction;&#64;Beforepublic void init(){Configuration configuration &#61; new Configuration().configure();ServiceRegistry serviceRegistry &#61; new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();sessionFactory &#61; configuration.buildSessionFactory(serviceRegistry);session &#61; sessionFactory.openSession();transaction &#61; session.beginTransaction();}&#64;Afterpublic void destory(){transaction.commit();session.close();sessionFactory.close();}&#64;Testpublic void testDoWork(){session.doWork(new Work() {&#64;Overridepublic void execute(Connection connection) throws SQLException {System.out.println(connection);}});}
}
结果&#xff1a;