两种开源数据库连接池的使用
来源:互联网 发布:mac免费视频剪辑软件 编辑:程序博客网 时间:2024/06/07 03:55
现在很多WEB服务器(Weblogic, WebSphere, Tomcat)都提供了DataSoruce的实现,即连接池的实现。通常我们把DataSource的实现,按其英文含义称之为数据源,数据源中都包含了数据库连接池的实现。开源组织提供了数据源的独立实现: DBCP 数据库连接池 C3P0 数据库连接池实际应用时不需要编写连接数据库代码,直接从数据源获得数据库的连接。程序员编程时也应尽量使用这些数据源的实现,以提升程序的数据库访问性能。
1.C3P0的使用:
a.拷贝c3p0-0.9.1.2.jar包和mysql-connector-java-5.0.8-bin.jar。
b.写配置文件。
c3p0-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?><c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///day16</property> <property name="user">root</property> <property name="password">admin</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </default-config> <named-config name="day15"> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </named-config></c3p0-config>
c.写工具类。Test.java文件:
package com.itheima.util;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3P0Util { private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } }}
4.使用(测试)。
package com.itheima.test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import com.itheima.util.C3P0Util;import com.itheima.util.DBCPUtil;public class Test { public static void main(String[] args) throws SQLException { Connection conn = C3P0Util.getConnection(); System.out.println(conn.getClass().getName()+"---"); conn.close(); }}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection---
2。DBCP的使用:
a.写数据库连接信息的配置文件。
#连接设置driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/testusername=rootpassword=sorry#<!-- 初始化连接 -->initialSize=10#最大连接数量maxActive=50#<!-- 最大空闲连接 -->maxIdle=20#<!-- 最小空闲连接 -->minIdle=5#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->maxWait=60000#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;] #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。connectionProperties=useUnicode=true;characterEncoding=utf8#指定由连接池所创建的连接的自动提交(auto-commit)状态。defaultAutoCommit=true#driver default 指定由连接池所创建的连接的只读(read-only)状态。#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)defaultReadOnly=#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLEdefaultTransactionIsolation=REPEATABLE_READ
b.拷贝三个jar包(commons-dbcp-1.4.jar和commons-pool-1.5.6.jar和mysql-connector-java-5.0.8-bin.jar).
c.写DBCP工具类。
package com.itheima.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSourceFactory;public class DBCPUtil { private static DataSource dataSource; static{ try { InputStream in = DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); Properties props = new Properties(); props.load(in); dataSource = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { throw new ExceptionInInitializerError(e); } } public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } }}
d.使用(测试)
package com.itheima.test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import com.itheima.util.C3P0Util;import com.itheima.util.DBCPUtil;public class Test { public static void main(String[] args) throws SQLException { Connection conn = DBCPUtil.getConnection(); System.out.println(conn.getClass().getName()+"---"); conn.close(); }}
运行结果:
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper—
0 0
- 两种开源数据库连接池的使用
- 数据库连接池的使用
- 数据库连接池的使用
- 数据库连接池的使用
- 数据库连接池的使用
- dataSource数据库连接池的使用
- dbcp数据库连接池的使用
- 数据库连接池使用的优点
- 数据库连接池libzdb的使用
- c3p0数据库连接池的使用
- oracle数据库连接池的使用
- DBCP数据库连接池的使用
- DBCP数据库连接池的使用
- c3p0数据库连接池的使用
- JDBC数据库连接池的使用
- c3p0数据库连接池的使用
- c3p0数据库连接池的使用
- c3p0数据库连接池的使用
- windows上使用eclipse远程连接hadoop进行程序开发没有权限访问问题
- vmare nat模式无法上网
- 2016Dalian网络预选赛1010 / HDU 5877 (dfs序+划分树)
- Jsoup解析Html之常用方法
- web service wsdl详解
- 两种开源数据库连接池的使用
- Storm实战演练:WordCont
- 低频信号为什么难以传输?
- 005-汇编加法指令ADD
- C++静态库与动态库的区别?
- nginx学习(四)——基础概念之keepAlive,pipe与长连接
- c++基础 (第九章)
- 某次体育比赛的结果有四种可能:胜(WIN)、负(LOSE)、平局(TIE)、比赛取消(CANCEL)编写程序输出这4种情况
- CODE CSDN Git 配制方法介绍