Hibernate配置c3p0连接池

来源:互联网 发布:温度测量仪软件下载 编辑:程序博客网 时间:2024/06/08 07:09

首先,我们需要导入c3p0的jar包



我们可以直接在官方文件中的project--->etc文件夹下找到hibernate.properties文件,在该文件下,

我们可以找到c3p0的配置的相关信息

###########################
### C3P0 Connection Pool###
###########################


#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false


然后我们就相应地在hibernate.cfg.xml文件中进行配置

<!-- 配置c3p0连接池 --><!-- 最大连接数 --><property name="hibernate.c3p0.max_size">20</property><!-- 最小连接数 --><property name="hibernate.c3p0.min_size">1</property><!-- 获取连接超时时间,若超过这个时间会抛出异常,单位:毫秒 --><property name="hibernate.c3p0.timeout">5000</property><!-- 创建PreparedStatement对象的最大数量 --><property name="hibernate.c3p0.max_statements">100</property><!-- 每个指定时间检查连接池中的空闲连接,时间单位:秒 --><property name="hibernate.c3p0.idle_test_period">150</property><!-- 当连接池使用完毕,c3p0再和数据库建立n个连接放置到连接池中 --><property name="hibernate.c3p0.acquire_increment">2</property><!-- 每次都验证连接是否可用 --><property name="hibernate.c3p0.validate">true</property>

然后我们运行程序


当你看到与c3p0相关的类似信息,说明配置成功了