最近在看有关Hibernate 数据库连接池的问题,确实数据连接池对应用程序的执行效率有很大的搞高,避免了频繁的数据库的连接工作。从GOOGLE中看到个各式各样的连接池的管理,大概总结了一下主要是以下几种。
一>,hibernate 自带的连接池,中要在Hibernate 配置文件 中加上 <property name="connection.pool_size">50</property>
即可,hibernate 的配置文件如下:
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > sa </ property >
< property name ="connection.url" >
jdbc:sqlserver://192.168.1.100:1433;databaseName=TestDB
</ property >
< property name ="dialect" >
org.hibernate.dialect.SQLServerDialect
</ property >
< property name ="connection.password" > sa </ property >
< property name ="connection.driver_class" > com.microsoft.sqlserver.jdbc.SQLServerDriver </ property >
< property name ="connection.pool_size" > 50 </ property >
<!-- 实体 -->
< mapping class ="com.data.entities.User" />
.......
</ session-factory >
</ hibernate-configuration >
但在 Hibernate 的官方文档中不支持这种做法因为hibernate 对连接池做的还不成熟有BUG,只在学习中可以用到,推荐用下面几种做法。
二>. Hibernate + JNDI +dbcp 连接池
通过JNDI中转DBCP连接池,将数据库操作的连接信息通过DBCP来存储管理。其具体配置如下:
hibernate.cfg.xml 配置如下是:
< session-factory >
< property name ="hibernate.dialect" > org.hibernate.dialect.SQLServerDialect </ property >
< property name ="connection.datasource" > java:comp/env/ jdbc/ManageDB </ property >
<!-- 实体映射 -->
<mapping class="com.data.entities.User" />
.....
</ session-factory >
</ hibernate-configuration >
在项目的webRoot/META-INF 文件夹下面新建 context.xml 添加如下内容
<! DOCTYPE XML >
< Context path ="/Manage" docBase ="Manage" debug ="5" reloadable ="true"
crossContext ="true" >
<!-- 数据库连接 begin -->
<!-- 被操作库 -->
< Resource name ="jdbc/ManageDB"
auth ="Container"
type ="javax.sql.DataSource"
maxActive ="5"
maxIdle ="5"
maxWait ="5000"
factory ="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName ="net.sourceforge.jtds.jdbc.Driver"
url ="jdbc:jtds:sqlserver://192.168.1.100:1433/TestDB"
username ="sa"
password ="sa"
testOnBorrow ="true"
testWhileIdle ="true"
validationQuery ="select getdate()" />
</ Context >
运行即可。(注,要引用两个JAR 文件 commons-dbcp-1.2.1.jar , commons-pool-1.2.jar 和 jtds-1.2.2.jar)
三>, Hibernate + proxool 连接池配置
1,先引用JAR 包 proxool-0.9.1.jar 和 proxool-cglib.jar ,
2,hibernate.cfg.xml
< session-factory >
< property name ="hibernate.connection.provider_class" >
org.hibernate.connection.ProxoolConnectionProvider
</ property >
< property name ="hibernate.proxool.pool_alias" > MyPool </ property >
< property name ="hibernate.proxool.xml" > proxool.xml </ property >
<!-- 实体映射 -->
< mapping class ="com.data.entities.User" />
.....
</ session-factory >
</ hibernate-configuration >
3,在同目录下面配置proxool.xml 文件 ,内容如下:
<!-- the proxool configuration can be embedded within your own application"s.
Anything outside the "proxool" tag is ignored. -->
< something-else-entirely >
< proxool >
< alias > MyPool </ alias >
< driver-url > jdbc:sqlserver://192.168.1.100:1433;databaseName=TestDB </ driver-url >
< driver-class > com.microsoft.sqlserver.jdbc.SQLServerDriver </ driver-class >
< driver-properties >
< property name ="user" value ="sa" />
< property name ="password" value ="sa" />
</ driver-properties >
< maximum-connection-count > 80 </ maximum-connection-count >
< minimum-connection-count > 20 </ minimum-connection-count >
< house-keeping-sleep-time > 180000 </ house-keeping-sleep-time >
< prototype-count > 5 </ prototype-count >
< house-keeping-test-sql > select CURRENT_DATE </ house-keeping-test-sql >
</ proxool >
</ something-else-entirely >
如果想到看proxool 连接池的监控界面就要在web.xml文件中做如下配置
< servlet-name > Admin </ servlet-name >
< servlet-class >
org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > Admin </ servlet-name >
< url-pattern > /proxool </ url-pattern >
</ servlet-mapping >
这样当你在浏览器中输入:http://localhost:8080/test/proxool 时就可看到你的配置信息和连接情况。如下图:
四,> hibernate + c3p0 连接池。
首先引用 c3p0-0.9.1.2.jar 包,然后将hibernate.cfg.xml 配置如下:
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > sa </ property >
< property name ="connection.url" > jdbc:sqlserver://192.168.1.100:1433;databaseName=TestDB </ property >
< property name ="dialect" > org.hibernate.dialect.SQLServerDialect </ property >
< property name ="connection.password" > sa </ property >
< property name ="hibernate.c3p0.max_size" > 50 </ property >
< property name ="hibernate.c3p0.min_size" > 5 </ property >
< property name ="hibernate.c3p0.timeout" > 100 </ property >
< property name ="hibernate.c3p0.idle_test_period" > 100 </ property >
< property name ="hibernate.c3p0.max_statements" > 150 </ property >
< property name ="hibernate.c3p0.acquire_increment" > 3 </ property >
< property name ="hibernate.connection.driver_class" > org.hibernate.dialect.SQLServerDialect </ property >
< property name ="hibernate.dialect" > org.hibernate.dialect.SQLServerDialect </ property >
< property name ="connection.provider_class" > org.hibernate.connection.C3P0ConnectionProvider </ property >
<!-- 实体 -->
< mapping class ="gk.data.entities.User" />
......
</ session-factory >
</ hibernate-configuration >
本人现就职于广州砺锋信息科技有限公司,欢迎咨询!
公司简介
砺锋科技,Java培训官方网站—Java培训,软件培训,人才外包服务
|全国Java培训和技术服务的领导者
|旨在循序渐进将零基础的人用最短的时间最有效的方法打造为 Java领域的顶级开发与创新型人才