Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP.在配置连接池时需要注意的有三点:
一、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代码:
- <?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 >
- <!—JDBC驱动程序-->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- 连接数据库的URL-->
- <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>
- <!--是否将运行期生成的SQL输出到日志以供调试-->
- <property name="show_sql">true</property>
- <!--指定连接的语言-->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!--映射Student这个资源-->
- <mapping resource="com/wqbi/model/pojo/student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
- <?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 >
- <!—JDBC驱动程序-->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- 连接数据库的URL-->
- <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>
- <!--是否将运行期生成的SQL输出到日志以供调试-->
- <property name="show_sql">true</property>
- <!--指定连接的语言-->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!--映射Student这个资源-->
- <mapping resource="com/wqbi/model/pojo/student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
2.C3P0连接配置
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 >
- <!—JDBC驱动程序-->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- 连接数据库的URL-->
- <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>
- <!-- C3P0连接池设定-->
- <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>
- <!--是否将运行期生成的SQL输出到日志以供调试-->
- <property name="show_sql">true</property>
- <!--指定连接的语言-->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!--映射Student这个资源-->
- <mapping resource="com/wqbi/model/pojo/student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
- <?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 >
- <!—JDBC驱动程序-->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- 连接数据库的URL-->
- <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>
- <!-- C3P0连接池设定-->
- <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>
- <!--是否将运行期生成的SQL输出到日志以供调试-->
- <property name="show_sql">true</property>
- <!--指定连接的语言-->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!--映射Student这个资源-->
- <mapping resource="com/wqbi/model/pojo/student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
3.proxool连接池
(1) 先写proxool的配置文件,文件名:proxool.xml(一般放在与hibernate.cfg.xml文件在同一个目录中)本例配置的是MYSQL数据库,数据库的名字为schoolproject
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>DBPool</alias>
- <!--proxool只能管理由自己产生的连接-->
- <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>
- <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回
- 收,超时的销毁-->
- <house-keeping-sleep-time>90000</house-keeping-sleep-time>
- <!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的
- 用户连接就不会被接受-->
- <maximum-new-connections>20</maximum-new-connections>
- <!-- 最少保持的空闲连接数-->
- <prototype-count>5</prototype-count>
- <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的
- 等待请求数由maximum-new-connections决定-->
- <maximum-connection-count>100</maximum-connection-count>
- <!-- 最小连接数-->
- <minimum-connection-count>10</minimum-connection-count>
- </proxool>
- </something-else-entirely>
- <?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>DBPool</alias>
- <!--proxool只能管理由自己产生的连接-->
- <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>
- <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回
- 收,超时的销毁-->
- <house-keeping-sleep-time>90000</house-keeping-sleep-time>
- <!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的
- 用户连接就不会被接受-->
- <maximum-new-connections>20</maximum-new-connections>
- <!-- 最少保持的空闲连接数-->
- <prototype-count>5</prototype-count>
- <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的
- 等待请求数由maximum-new-connections决定-->
- <maximum-connection-count>100</maximum-connection-count>
- <!-- 最小连接数-->
- <minimum-connection-count>10</minimum-connection-count>
- </proxool>
- </something-else-entirely>
(2)配置hibernate.cfg.xml文件
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>
- <!--是否将运行期生成的SQL输出到日志以供调试-->
- <property name="show_sql">true</property>
- <!--指定连接的语言-->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!--映射Student这个资源-->
- <mapping resource="com/wqbi/model/pojo/student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
- <?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>
- <!--是否将运行期生成的SQL输出到日志以供调试-->
- <property name="show_sql">true</property>
- <!--指定连接的语言-->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!--映射Student这个资源-->
- <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 连接池
数据源已经由应用服务配置好(如Web服务器),Hibernate需要做的只是通过JNDI名查找到此数据源.应用服务器将连接池对外显示为 JNDI绑定数据源,它是javax.jdbc.Datasource类的一个实例.只要配置一个Hibernate文件,如:
- hibernate.connection.datasource=java:/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
【编辑推荐】