连接池,
作用: 管理连接;提升连接的利用效率!
常用的连接池: C3P0连接池
Hibernate自带的也有一个连接池,且对C3P0连接池也有支持!
Hbm 自带连接池:
只维护一个连接,比较简陋。
可以查看hibernate.properties文件查看连接池详细配置:
#################################
### Hibernate Connection Pool ###
#################################
hibernate.connection.pool_size 1 【Hbm 自带连接池: 只有一个连接】
###########################
### C3P0 Connection Pool### 【Hbm对C3P0连接池支持】
###########################
#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
【Hbm对C3P0连接池支持, 核心类】
告诉hib使用的是哪一个连接池技术。
#hibernate.connection.provider_classorg.hibernate.connection.C3P0ConnectionProvider
下面是在hibernate.cfg.xml中增加数据库连接池的详细配置信息
<!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节点代表一个数据库 -->
<session-factory>
<!-- 1.数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib-demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!--数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2.其他相关配置 -->
<!--2.1显示hibernate在运行的时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--****************** 【连接池配置】****************** -->
<!-- 配置连接驱动管理类 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 配置连接池参数信息 -->
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">4</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.idle_test_period">30000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 3.加载所有映射
<mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>