最近在看有关Hibernate 数据库连接池的问题,确实数据连接池对应用程序的执行效率有很大的搞高,避免了频繁的数据库的连接工作。从GOOGLE中看到个各式各样的连接池的管理,大概总结了一下主要是以下几种。
一>,hibernate 自带的连接池,中要在Hibernate 配置文件 中加上 <property name="connection.pool_size">50</property>
即可,hibernate 的配置文件如下:
<?
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"
>
<!--
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 配置如下是:
<
hibernate-configuration
>
<
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 添加如下内容
<?
xml version="1.0" encoding="UTF-8"
?>
<!
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
<
hibernate-configuration
>
<
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 文件 ,内容如下:
代码
<?
xml version="1.0" encoding="UTF-8"
?>
<!--
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
>
<
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 配置如下:
<?
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"
>
<!--
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
>
如此,这4种方式的连接池即如此,上面的配置已经测试过没有可以连接成功,不才在此班门弄斧,还不知道怎样测试这几种边连接池效率如何呢,如有路过的朋友知道还望指点一二,谢谢,欢迎大家拍砖。