数据库连接池DBCP和C3P0
2015-08-02 23:11
549 查看
直接代码:
Spring中:
相关网址:
DBCP百度百科
DBCP官网教程
C3P0百度百科
C3P0官网教程
public class DBCPUtils { private static DBCPUtils dbcputils=null; private BasicDataSource bds=null; private DataSourceConnectionFactory dscf=null; private DBCPUtils(){ if(bds==null) bds=new BasicDataSource(); bds.setUrl(DBConsts.url); bds.setUsername(DBConsts.username); bds.setPassword(DBConsts.password); bds.setDriverClassName(DBConsts.driverclass); bds.setMaxActive(100); bds.setInitialSize(20); bds.setMaxIdle(20); bds.setMinIdle(10); dscf=new DataSourceConnectionFactory(bds); } public synchronized static DBCPUtils getInstance(){ if(dbcputils==null) dbcputils=new DBCPUtils(); return dbcputils; } public Connection getConnection(){ Connection con=null; try { con=(Connection)dscf.createConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static void main(String[] args) throws SQLException { Connection con=null; long begin=System.currentTimeMillis(); for(int i=0;i<1000000;i++){ con=DBCPUtils.getInstance().getConnection(); con.close(); } long end=System.currentTimeMillis(); System.out.println("耗时为:"+(end-begin)+"ms"); } }
public class C3P0Utils { private static C3P0Utils dbcputils=null; private ComboPooledDataSource cpds=null; private C3P0Utils(){ if(cpds==null){ cpds=new ComboPooledDataSource(); } cpds.setUser(DBConsts.username); cpds.setPassword(DBConsts.password); cpds.setJdbcUrl(DBConsts.url); try { cpds.setDriverClass(DBConsts.driverclass); } catch (PropertyVetoException e) { // TODO Auto-generated catch block e.printStackTrace(); } cpds.setInitialPoolSize(100); cpds.setMaxIdleTime(20); cpds.setMaxPoolSize(100); cpds.setMinPoolSize(10); } public synchronized static C3P0Utils getInstance(){ if(dbcputils==null) dbcputils=new C3P0Utils(); return dbcputils; } public Connection getConnection(){ Connection con=null; try { con=cpds.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static void main(String[] args) throws SQLException { Connection con=null; long begin=System.currentTimeMillis(); for(int i=0;i<1000000;i++){ con=C3P0Utils.getInstance().getConnection(); con.close(); } long end=System.currentTimeMillis(); System.out.println("耗时为:"+(end-begin)+"ms"); } }
Spring中:
<!-- 配置dbcp数据源 --> <bean id="dataSource2" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 池启动时创建的连接数量 --> <property name="initialSize" value="5"/> <!-- 同一时间可以从池分配的最多连接数量。设置为0时表示无限制。 --> <property name="maxActive" value="30"/> <!-- 池里不会被释放的最多空闲连接数量。设置为0时表示无限制。 --> <property name="maxIdle" value="20"/> <!-- 在不新建连接的条件下,池中保持空闲的最少连接数。 --> <property name="minIdle" value="3"/> <!-- 设置自动回收超时连接 --> <property name="removeAbandoned" value="true" /> <!-- 自动回收超时时间(以秒数为单位) --> <property name="removeAbandonedTimeout" value="200"/> <!-- 设置在自动回收超时连接的时候打印连接的超时错误 --> <property name="logAbandoned" value="true"/> <!-- 等待超时以毫秒为单位,在抛出异常之前,池等待连接被回收的最长时间(当没有可用连接时)。设置为-1表示无限等待。 --> <property name="maxWait" value="100"/> </bean> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="100" /> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="1" /> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="10" /> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="30" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5" /> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0--> <property name="maxStatements" value="0" /> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60" /> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="30" /> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false--> <property name="breakAfterAcquireFailure" value="true" /> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout" value="false" /> </bean>
相关网址:
DBCP百度百科
DBCP官网教程
C3P0百度百科
C3P0官网教程
相关文章推荐
- MongoDb 命令查询所有数据库列表
- 8天学通MongoDB——第一天 基础入门
- Hibernate学习(9)数据库事务
- 在Ubuntu 14.04上安装MariaDB
- 2015年辛星Redis教程发布,是win版奥
- mysql常用命令
- 关于nutch2.x不支持mysql
- InnoDB中MVCC的实现
- mongodb的查询语句学习摘要
- mongodb实现简单的增删改查
- java/oracle日期处理
- Oracle RAC安装过程中碰到的“坑”和关键点(二)
- mssql:sp_executesql与exec(@sql)
- 风清杨之Oracle的安装与说明
- window上安装 MongoDB PHP扩展
- Oracle学习之DATAGUARD(一) DG架构
- MongoDB大文件存储流程
- Oracle经典20题
- 使用wamp集成开发环境,dos命令下数据库乱码解决问题
- ubuntu-12.04.5-desktop-i386.iso:ubuntu-12.04.5-desktop-i386:安装Oracle11gR2