Hibernate之连接池配置

xiaoxiao2021-02-27  298

一,概述

1)之前在一篇博客里介绍了C3P0连接池组件的用法,现在学习Hibernate,又与连接池见面了.连接池的作用就是管理连接,提升连接的利用效率.

2)Hibernate自带的也有一个连接池,只维护了一个连接,比较简陋.在hibernate.properties可以查看得到hibernate自维护的连接池:

################################# ### Hibernate Connection Pool ### ################################# hibernate.connection.pool_size 1

3)在hibernate.properties查看得知,hibernate对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从上到下6个表示意思依次为:

最大连接数 最小连接数 超时时间 最大执行的命令的个数 空闲测试时间 连接不够用的时候, 每次增加的连接数

4)同样在hibernate.properties文件中可以看到,不同的连接池技术,配置有区别

#hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider

二,配置hibernate.cfg.xml

1)配置文件

<!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">abc</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.max_size">5</property> <!-- 最大连接数 --> <property name="hibernate.c3p0.min_size">2</property> <!-- 最小连接数 --> <property name="hibernate.c3p0.timeout">5000</property> <!-- 超时时间 --> <property name="hibernate.c3p0.max_statements">100</property> <!-- 最大执行的命令格个数 --> <property name="hibernate.c3p0.idle_test_period">30000</property> <!-- 空闲测试时间 --> <property name="hibernate.c3p0.acquire_increment">2</property> <!-- 连接不够用时,每次增加的个数 --> <!-- #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 --> </session-factory> </hibernate-configuration> 2)hibernate使用C3P0连接池,除了hibernate的jar包,当然还要导入C3P0jar包了(如:c3p0-0.9.1.2.jar),否则报错.

转载请注明原文地址: https://www.6miu.com/read-1766.html

最新回复(0)