hibernate 做数据连接池的几种方式
来源:互联网 发布:无人深空低配优化 编辑:程序博客网 时间:2024/05/21 22:54
hibernate 做数据连接池的几种方式:
配置Hibernate使用c3p0或Proxool连接池作者:admin 日期:2006-12-20
1.Hibernate默认连接池
<?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="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</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 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 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 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
5 hibernate.properties 与hibernate.cfg.xml联合,效果雷同于方法1 和 2
(1) hibernate.cfg.xml配置:主要是ORM映射文件
<?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>
<!-- local connection properties -->
<mapping resource="com/etraveltek/frame/org/entity/Organization.hbm.xml" />
<mapping resource="com/etraveltek/frame/org/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
(2) hibernate.properties 配置,以下用到了c3p0
######################
### Query Language ###
######################
## define query language constants / function names
hibernate.query.substitutions yes 'Y', no 'N'
#################
### Platforms ###
#################
## MySQL
#hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://10.10.8.12/abt?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
hibernate.connection.username dev
hibernate.connection.password devetravel
#################################
### Hibernate Connection Pool ###
#################################
hibernate.connection.pool_size 10
###########################
### C3P0 Connection Pool###
###########################
hibernate.c3p0.max_size 10
hibernate.c3p0.min_size 4
hibernate.c3p0.timeout 600
hibernate.c3p0.max_statements 50
hibernate.c3p0.idle_test_period 3000
hibernate.c3p0.acquire_increment 2
hibernate.c3p0.validate false
hibernate.c3p0.testConnectionOnCheckout false
#################################
### Plugin ConnectionProvider ###
#################################
## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics)
#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
##############################
### Miscellaneous Settings ###
##############################
## print all generated SQL to the console
#hibernate.show_sql true
## format SQL in log and console
hibernate.format_sql true
## set the maximum depth of the outer join fetch tree
hibernate.max_fetch_depth 1
#####################
### JDBC Settings ###
#####################
## set the JDBC fetch size
#hibernate.jdbc.fetch_size 25
hibernate.jdbc.fetch_size 100
## set the maximum JDBC 2 batch size (a nonzero value enables batching)
hibernate.jdbc.batch_size 50
## enable batch updates even for versioned data
hibernate.jdbc.batch_versioned_data true
## use streams when writing binary types to / from JDBC
hibernate.jdbc.use_streams_for_binary true
##########################
### Second-level Cache ###
##########################
## set a prefix for cache region names
hibernate.cache.region_prefix hibernate.test
hibernate.cache.use_query_cache true
## choose a cache implementation
#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider
hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider
#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider
- hibernate 做数据连接池的几种方式
- hibernate数据查询的几种方式
- hibernate数据查询的几种方式
- hibernate数据查询的几种方式
- Hibernate查询数据的几种方式:
- hibernate几种连接池的比较
- hibernate 数据库几种连接数据
- hibernate模糊查询的几种方式
- Hibernate的几种查询方式
- hibernate 的几种查询方式 一
- Hibernate的其他几种检索方式:
- hibernate访问数据库的几种方式
- Hibernate的几种查询方式
- Hibernate的几种查询方式
- Hibernate的几种主要检索方式
- Hibernate事务的几种配置方式
- Hibernate 检索查询的几种方式
- 初识Hibernate的几种方式
- 密码学里的随机数发生器
- firefox和thunderbird的内存占用过大问题
- 不正常地定义参数对象。提供了不一致或不完整的信息
- 有关mysql连接慢的问题
- delphi内如何检测用户多长时间没有鼠标与键盘操作?
- hibernate 做数据连接池的几种方式
- SQL Server 安装时报有文件被挂起,不能安装
- IIS 启动不了 【发生意外错误0x8ffe2740】
- 使用IdHTTP的问题 Object Moved
- 目录选择对话框
- MiddlegenIDE的使用
- 使用ShLwApi中的PathCompactPathEx进行路径压缩显示
- delphi内进行音量控制及静音
- Delphi中如何准确截取汉字