经过前面的学习,已经掌握了hibernate的基本使用。本篇讲的是hibernate使用数据库连接池的配置。
1、使用C3P0
- 导入jar包,
hibernate-release-5.2.10.Final\lib\optional\c3p0
文件夹下
- 配置hibernate.cfg.xml文件,直接贴出完整配置文件
<?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-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 绑定本地session 放在本地线程中 共三个值 一般用thread与本地线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- C3P0连接池配置 -->
<!-- 丢弃 不必配置即可使用 property name="hibernate.connection.provider_class">
org.hibernate.c3p0.internal.C3P0ConnectionProvider
</property -->
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">50</property>
<!-- 连接的最大空闲时间,1800秒内未使用则连接被丢弃。单位为秒 -->
<property name="hibernate.c3p0.timeout">1800</property>
<!-- 每隔120秒检查连接池里的空闲连接是否超时,单位是秒 -->
<property name="hibernate.c3p0.idle_test_period">120</property>
<mapping resource="cn/lkangle/entity/onetwo.hbm.xml"/>
<mapping resource="cn/lkangle/entity/three.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 测试使用C3P0
控制台输出如上信息说明C3P0使用成功。END
在源码中发现hibernate中C3P0有效的配置也就已上六个,而我百度到的还发现这样一条配置
<property name="hibernate.c3p0.validate">true</property>
设置为true每次都检测连接是否可用。没有测试不知道是否有用。
下面是AvailableSettings
类中关于C3P0的配置部分代码
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// c3p0 connection pooling specific settings
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* A setting prefix used to indicate settings that target the hibernate-c3p0 integration
*/
String C3P0_CONFIG_PREFIX = "hibernate.c3p0";
/**
* Maximum size of C3P0 connection pool
*/
String C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
/**
* Minimum size of C3P0 connection pool
*/
String C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
/**
* Maximum idle time for C3P0 connection pool
*/
String C3P0_TIMEOUT = "hibernate.c3p0.timeout";
/**
* Maximum size of C3P0 statement cache
*/
String C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
/**
* Number of connections acquired when pool is exhausted
*/
String C3P0_ACQUIRE_INCREMENT = "hibernate.c3p0.acquire_increment";
/**
* Idle time beforeQuery a C3P0 pooled connection is validated
*/
String C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";