hibernate 支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP。在Hibernate连接池配置时需要注意的有三点:
一、Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已经不再推荐使用,官方的解释是这个连接池存在缺陷。如果你因为某种原因需要在Hibernate3中使用DBCP,建议采用JNDI方式。
二、默认情况下(即没有配置连接池的情况下),Hibernate会采用内建的连接池。但这个连接池性能不佳,且存在诸多BUG(笔者就曾在MySQL 环境下被八小时连接悬空问题困扰过),因此官方也只是建议仅在开发环境下使用。
三、Hibernate2和Hibernate3的命名空间有所变化。例如,配置C3P0时的provider_class有Hibernate2环境下使用net.sf.hibernate.connection.C3P0ConnectionProvider,在Hibernate3环境下使用org.hibernate.connection.C3P0ConnectionProvider。
下面是Hibernate环境下几种常见的连接池配置:
1.默认Hibernate连接池配置
<? xml version = '1.0' encoding = 'UTF-8' ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge < a href = "http://lib.csdn.net/base/dotnet" class = 'replace_word' title = ".NET知识库" target = '_blank' style = 'color:#df3434; font-weight:bold;' > .NET </ a > /hibernate-configuration-3.0.dtd" > < hibernate-configuration > < session-factory > <!—JDBC驱动程序--> < property name = "connection.driver_class" > com.mysql.jdbc.Driver </ property > < property name = "connection.url" > jdbc:mysql://localhost:3306/schoolproject </ property > < property name = "connection.useUnicode" > true </ property > < property name = "connection.characterEncoding" > UTF-8 </ property > < property name = "connection.username" > root </ property > <!—登录密码--> < property name = "connection.password" > </ property > < property name = "show_sql" > true </ property > < property name = "dialect" > org.hibernate.dialect.MySQLDialect </ property > < mapping resource = "com/wqbi/model/pojo/student.hbm.xml" /> </ session-factory > </ hibernate-configuration >
2.C3P0的Hibernate连接池配置
<? xml version = '1.0' encoding = 'UTF-8' ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge < a href = "http://lib.csdn.net/base/dotnet" class = 'replace_word' title = ".NET知识库" target = '_blank' style = 'color:#df3434; font-weight:bold;' > .Net </ a > /hibernate-configuration-3.0.dtd" > < hibernate-configuration > < session-factory > <!—JDBC驱动程序--> < property name = "connection.driver_class" > com.mysql.jdbc.Driver </ property > < property name = "connection.url" > jdbc:mysql://localhost:3306/schoolproject </ property > < property name = "connection.useUnicode" > true </ property > < property name = "connection.characterEncoding" > UTF-8 </ property > < property name = "connection.username" > root </ property > < property name = "connection.password" > </ property > < property name = "hibernate.connection.provider_class" > org.hibernate.connection.C3P0ConnectionProvider </ property > < property name = "hibernate.c3p0.max_size" > 20 </ property > < property name = "hibernate.c3p0.min_size" > 5 </ property > < property name = "hibernate.c3p0.timeout" > 120 </ property > < property name = "hibernate.c3p0.max_statements" > 100 </ property > < property name = "hibernate.c3p0.idle_test_period" > 120 </ property > < property name = "hibernate.c3p0.acquire_increment" > 2 </ property > < property name = "show_sql" > true </ property > < property name = "dialect" > org.hibernate.dialect.MySQLDialect </ property > < mapping resource = "com/wqbi/model/pojo/student.hbm.xml" /> </ session-factory > </ hibernate-configuration >
3.Proxool0的Hibernate连接池配置
(1)配置proxoolconf.xml文件
<? xml version = "1.0" encoding = "UTF-8" ?> < something-else-entirely > < proxool > < alias > DBPool </ alias > < driver-url > jdbc:mysql://localhost:3306/schoolproject? useUnicode = true & characterEncoding = UTF8 </ driver-url > <!—JDBC驱动程序--> < driver-class > com.mysql.jdbc.Driver </ driver-class > < driver-properties > < property name = "user" value = "root" /> < property name = "password" value = "" /> </ driver-properties > < house-keeping-sleep-time > 90000 </ house-keeping-sleep-time > < maximum-new-connections > 20 </ maximum-new-connections > < prototype-count > 5 </ prototype-count > < maximum-connection-count > 100 </ maximum-connection-count > < minimum-connection-count > 10 </ minimum-connection-count > </ proxool > </ something-else-entirely >
(2)配置hibernate.cfg.xml文件
<? xml version = '1.0' encoding = 'UTF-8' ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > < hibernate-configuration > < session-factory > < property name = "hibernate.connection.provider_class" > org.hibernate.connection.ProxoolConnectionProvider </ property > < property name = "hibernate.proxool.pool_alias" > DBPool </ property > < property name = "hibernate.proxool.xml" > proxoolconf.xml </ property > < property name = "show_sql" > true </ property > < property name = "dialect" > org.hibernate.dialect.MySQLDialect </ property > < mapping resource = "com/wqbi/model/pojo/student.hbm.xml" /> </ session-factory > </ hibernate-configuration >
(1) hibernate.connection.provider_class定义Hibernate的连接加载类,这里Proxool连接池是用这个,不同 的连接池有不同的加载类,可以查阅Hibernate文档获取相关信息
(2) hibernate.proxool.pool_alias这里就是用我们上面提到的连接池的别名
(3) hibernate.proxool.xml是向Hibernate声明连接池的配置文件位置,可以用相对或绝对路径,用相对路径时要注意一定在要Path范围内!不然会抛出异常。
(4) dialect是声明SQL语句的方言
(5) show_sql定义是否显示Hibernate生成的SQL语言,一般在调试阶段设为true,完成后再改成false,这样有利于调试
(6) <mapping >资源文件映射
4. JNDI方式的Hibernate连接池配置
数据源已经由应用服务配置好(如Web服务器),Hibernate需要做的只是通过JNDI名查找到此数据源。应用服务器将连接池对外显示为JNDI绑定数据源,它是javax.jdbc.Datasource类的一个实例。只要配置一个Hibernate文件,如:hibernate.properties
hibernate.connection.datasource = < a href = "http://lib.csdn.net/base/javase" class = 'replace_word' title = "Java SE知识库" target = '_blank' style = 'color:#df3434; font-weight:bold;' > Java </ a > :/comp/env/jdbc/schoolproject //JNDI名 hibernate.transaction.factory_class = org .hibernate.transaction.JTATransactionFactory hibernate.transaction.manager_loopup_class = org .hibernate.transaction.JBossTransactionManagerLookup hibernate.dialect = org .hibernate.dialect.MySQLDialect
结论:
由于在Hibernate3.0中,已经不再支持dbcp了,Hibernate的作者在hibernate.org中,明确指出在实践中发现dbcp有 BUG,在某些种情会产生很多空连接不能释放,所以抛弃了对dbcp的支持。我不知是否在dbcp最新版本中解决了这个问题,我以前在一个访问量不大的项目中用过dbcp,运行了一年多都没出现问题。不过在网上的确也有不少网友指出dbcp在大型的应用中会出现不稳定的情况。所以在真相未经证实的情况下,我觉得对dbcp持慎重的态度。
至于c3p0,有评论说它的算法 不是最优的,而且,我在matrix中,见到有网友做了一个实验,在同一项目中分别用了几个常用的Hibernate连接池配置,然后测试 其性能,发现c3p0占用资源比较大,效率也不高。
所以,基于上述原因,我才花两晚的时间去研究proxool的配置,proxool不少行家推荐使用,而且暂时来说,是负面评价是最少的一个。在三星中也有项目是用proxool的。
最后
c3p0.max_statements不设置等于0时报如下错误
< property name = "hibernate.c3p0.max_size" > 20 </ property > < property name = "hibernate.c3p0.min_size" > 5 </ property > < property name = "hibernate.c3p0.timeout" > 120 </ property > < property name = "hibernate.c3p0.max_statements" > 0 </ property > < property name = "hibernate.c3p0.idle_test_period" > 120 </ property > < property name = "hibernate.c3p0.acquire_increment" > 2 </ property > < property name = "hibernate.c3p0.validate" > true </ property >
INFO 2010 -07 -23 16 :29 :38 ,490 [com.mchange.v2.c3p0.stmt.GooGooStatementCache] - Problem with checked-in Statement, discarding. java.lang.NullPointerException