连接池有那几种 分别有什么区别
2012-06-03 21:37
302 查看
在讲解连接池之前,先弄懂以下几个概念:
数据源:Data source , 读取数据的来源,也就是说你的系统要读取的数据去什么地方找(存放数据的地方)!
JDBC:全称为Java DataBase Connectivity standard, 它是一个面向对象的应用程序接口(API), 通过它可访问各类关系数据库。
两者之间的关系:数据源并不等于连接池,数据源不是必须要求实现连接池的,即连接池是数据源的一种.
c3p0: 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 ><!--JDBC驱动程序--><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- 连接数据库的URL--><property name="connection.url">jdbc:mysql://localhost:3306/application_context </property><property name="connection.useUnicode">true</property><property name="connection.characterEncoding">UTF-8</property><!--连接的登录名--><property name="connection.username">root</property><!--登录密码--><property name="connection.password">paul</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><!-- 最大的PreparedStatement的数量 --><property name="hibernate.c3p0.max_statements">100</property><!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒--><property name="hibernate.c3p0.idle_test_period">120</property><!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 --><property name="hibernate.c3p0.acquire_increment">2</property>
<!--其它配置-->
……
</session-factory></hibernate-configuration>
dbcp: hibernate.cfg.xml中配置<property name="dbcp.maxActive">100</property><property name="dbcp.whenExhaustedAction">1</property><property name="dbcp.maxWait">60000</property><property name="dbcp.maxIdle">10</property><property name="dbcp.ps.maxActive">100</property><property name="dbcp.ps.whenExhaustedAction">1</property><property name="dbcp.ps.maxWait">60000</property><property name="dbcp.ps.maxIdle">10</property>
proxool: hibernate.cfg.xml中配置
先写proxool的配置文件,文件名:proxool.xml(一般放在与hibernate.cfg.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/application_context?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>
配置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></session-factory></hibernate-configuration>
.JNDI: hibernate.cfg.xml中配置
JNDI连接池,数据源已经由应用服务配置好(如Web服务器),Hibernate需要做的只是通过JNDI名查找到此数据源。应用服务器将连接池对外显示为JNDI绑定数据源,它是javax.jdbc.Datasource类的一个实例。只要配置一个Hibernate文件,如:hibernate.connection.datasource=java:/comp/env/jdbc/JNDI_NAME //JNDI名hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactoryhibernate.transaction.manager_loopup_class=org.hibernate.transaction.JBossTransactionManagerLookuphibernate.dialect=org.hibernate.dialect.MySQLDialect区别:在hibernate3.0中,已经不再支持dbcp了,hibernate的作者在hibernate.org中,明确指出在实践中发现 dbcp有BUG,在某些种情会产生很多空连接不能释放,所以抛弃了对dbcp的支持。proxool不少行家推荐使用.c3p0占用资源比较大,效率也不高.dbcp没有自动的去回收空闲连接的功能c3p0有自动回收空闲连接功能
相关文章推荐
- Java - Session的save()、update()、merge()、lock()、saveOrUpdate()和persist()方法分别是做什么的?有什么区别?
- windows的oem跟vlk分别是什么意思?区别是什么?
- delegate和notification区别,分别在什么情况下使用?
- GB2312, GBK, GB18030 这几种字符集主要的区别是什么?
- 两种跳转方式分别是什么?有什么区别
- EVDO/GSM+WCDMA和WCDMA有什么区别分别是做什么
- 方法重写和方法重载的区别?this关键字和super关键字分别代表什么?
- QT中LineEdit TextEdit PlainTextEdit 这三个控件有什么区别,分别用在什么情况下呢
- 类成员变量和实例成员变量分别指什么有什么区别?
- plist文件里的"Bundle versions string, short" 跟 "Bundle version" 的区别及作用分别是什么?
- 两种跳转方式分别是什么?有什么区别
- 类中包含几种成员,分别是什么?
- asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别
- apache有几种工作模式,分别介绍下其特点,并说明什么情况下采用不同的工作模式?
- asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别
- git、github、gitlib、gitlab分别是什么,有什么区别?
- NTFS和FAT32分别是什么意思?他们两有什么区别?各自的作用分别是什么?
- 继承有几种方式,分别是什么,想要实现继承可以使用哪些方法
- C-013.break和continue的作用和区别,它们的使用场景分别是什么?
- gradle和maven有什么用?分别有什么区别?