Spring c3p0支持Hibernate配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 该类是hibernate-c3p0-5.2.6.Final中的-->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> <!-- 设置最小连接数 --> <property name="hibernate.c3p0.min_size">5</property> <!-- 设置最大连接数 --> <property name="hibernate.c3p0.max_size">15</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=GBK</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/maya/model/TStudent.hbm.xml" /> </session-factory> </hibernate-configuration>
package com.maya.test; import java.util.Calendar; import java.util.List; import org.hibernate.Session; import com.maya.model.TStudent; public class TestHibernate { public static void main(String[] args) { long start = Calendar.getInstance().getTimeInMillis(); for(int i=0;i<1000;i++){ Session session = HibernateUtil.getSession(); List<TStudent> list = session.createQuery("from TStudent").getResultList(); for(TStudent data:list){ System.out.println(data); } HibernateUtil.closeSession(); } long end = Calendar.getInstance().getTimeInMillis(); System.out.println(end-start); } }
对于hibernate来说用与不用连接池的链接效率几乎没什么不同