我的需求:配置c3p0的最大连接数是2,最小连接数是1,然后在程序中想测试这个功能,故获取多个连接想让它报错,但是它就是不报错,很费解,可能是我的测试方法不对,各位老手帮我看一下怎么回事吧。
先看看我的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"> <!-- hibernate配置 --> <hibernate-configuration> <session-factory> <!-- 数据库方言 --> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 数据库连接字符串 --> <property name="connection.url"> jdbc:mysql://localhost/java_demo </property> <!-- 数据库用户名 --> <property name="connection.username">root</property> <!-- 密码 --> <property name="connection.password">root</property> <!-- 数据库驱动 --> <property name="connectiosn.driver_class"> com.mysql.jdbc.Driver </property> <!-- 是否显示执行的sql语句 --> <property name="show_sql">true</property> <!-- c3p0配置 --> <!-- 指定connection的提供者 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.max_size">2</property> <property name="hibernate.c3p0.min_size">1</property> <property name="hibernate.c3p0.timeout">1</property> <property name="hibernate.c3p0.max_statements">1</property> <property name="hibernate.c3p0.validate">false</property> <property name="hibernate.c3p0.acquire_increment">0</property> <property name="hibernate.c3p0.idle_test_period">1</property> <!-- 配置映射文件 --> <mapping resource="hibernate/c3p0/entity/Student.hbm.xml" /> </session-factory> </hibernate-configuration>
我的测试代码
public static void main(String[] args) { Configuration config=new Configuration().configure(); SessionFactory factory=config.buildSessionFactory(); //获取4个session Session session=factory.openSession(); Session session2=factory.openSession(); Session session3=factory.openSession(); Session session4=factory.openSession(); //获取4个Connection对象 Connection c1=session.connection(); Connection c2=session2.connection(); Connection c3=session3.connection(); Connection c4=session4.connection(); }
如果测试代码没有问题的话是不是hibernate配置的c3p0不对?
另外有个问题问下,hibernate配置c3p0下面两个配置有什么区别
<property name="hibernate.c3p0.max_size">2</property>
<property name="c3p0.max_size">2</property>