zoukankan      html  css  js  c++  java
  • 连接池

    在对数据库进行操作的时候,会出现频繁的连接打开和关闭的情况;数据库连接资源非常宝贵,为了避免频繁连接对运行效率的影响就出现了连接池技术。

    常用的连接池技术有:

    1、DBCP

    2、C3P0

    以上连接池的实现是对Sun公司提供的连接池接口(javax.sql.DataSource)的实现。

    DBCP连接池

    DBCP 是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文件:
      1、Commons-dbcp.jar:连接池的实现
      2、Commons-pool.jar:连接池实现的依赖库
    Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。

    核心类:BasicDataSource

    需要引入的jar文件:commons-dbcp-1.4.jar 、 commons-pool-1.5.6.jar

    DBCP连接池API及使用示例

    1、基本使用-硬编码方式

     1 public void testDbcp() throws Exception {
     2         // DBCP连接池核心类
     3         BasicDataSource dataSouce = new BasicDataSource();
     4         // 连接池参数配置:初始化连接数、最大连接数 / 连接字符串、驱动、用户、密码
     5         dataSouce.setUrl("jdbc:mysql:///jdbc_demo");            //数据库连接字符串
     6         dataSouce.setDriverClassName("com.mysql.jdbc.Driver");  //数据库驱动
     7         dataSouce.setUsername("root");                            //数据库连接用户
     8         dataSouce.setPassword("root");                             //数据库连接密码
     9         dataSouce.setInitialSize(3);  // 初始化连接
    10         dataSouce.setMaxActive(6);      // 最大连接
    11         dataSouce.setMaxIdle(3000);   // 最大空闲时间
    12         
    13         // 获取连接
    14         Connection con = dataSouce.getConnection();
    15         con.prepareStatement("delete from admin where id=3").executeUpdate();
    16         // 关闭
    17         con.close();
    18     }
    19     
    View Code

    2、从配置文件中读取配置

     1 // 2. 【推荐】配置方式实现连接池  ,  便于维护
     2     public void testProp() throws Exception {
     3         // 加载prop配置文件
     4         Properties prop = new Properties();
     5         // 获取文件流
     6         InputStream inStream = App_DBCP.class.getResourceAsStream("db.properties");
     7         // 加载属性配置文件
     8         prop.load(inStream);
     9         // 根据prop配置,直接创建数据源对象
    10         DataSource dataSouce = BasicDataSourceFactory.createDataSource(prop);
    11         
    12         // 获取连接
    13         Connection con = dataSouce.getConnection();
    14         con.prepareStatement("delete from admin where id=4").executeUpdate();
    15         // 关闭
    16         con.close();
    17     }
    18 }
    View Code

    注意:配置文件中的key与BaseDataSource中的属性一样。

    例如:

    url=jdbc:mysql:///jdbc_demo
    driverClassName=com.mysql.jdbc.Driver
    username=root
    password=root
    initialSize=3
    maxActive=6
    maxIdle=3000

    C3P0连接池

    最常用的连接池技术!Spring框架,默认支持C3P0连接池技术!

    核心类:CombopooledDataSource

    引入的jar文件:c3p0-0.9.1.2.jar

    常用API及示例

    1、硬编码方式

     1 //1. 硬编码方式,使用C3P0连接池管理连接 
     2     public void testCode() throws Exception {
     3         // 创建连接池核心工具类
     4         ComboPooledDataSource dataSource = new ComboPooledDataSource();
     5         // 设置连接参数:url、驱动、用户密码、初始连接数、最大连接数
     6         dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_demo");
     7         dataSource.setDriverClass("com.mysql.jdbc.Driver");
     8         dataSource.setUser("root");
     9         dataSource.setPassword("root");
    10         dataSource.setInitialPoolSize(3);
    11         dataSource.setMaxPoolSize(6);
    12         dataSource.setMaxIdleTime(1000);
    13         
    14         // ---> 从连接池对象中,获取连接对象
    15         Connection con = dataSource.getConnection();
    16         // 执行更新
    17         con.prepareStatement("delete from admin where id=7").executeUpdate();
    18         // 关闭
    19         con.close();
    20     }
    View Code

    2、XML配置方式

     1 //2. XML配置方式,使用C3P0连接池管理连接
     2     public void testXML() throws Exception {
     3         // 创建c3p0连接池核心工具类
     4         // 自动加载src下c3p0的配置文件【c3p0-config.xml】
     5         ComboPooledDataSource dataSource = new ComboPooledDataSource();// 使用默认的配置
     6         PreparedStatement pstmt = null;
     7         
     8         // 获取连接
     9         Connection con = dataSource.getConnection();
    10         for (int i=1; i<11;i++){
    11             String sql = "insert into employee(empName,dept_id) values(?,?)";
    12             // 执行更新
    13             pstmt = con.prepareStatement(sql);
    14             pstmt.setString(1, "Rose" + i);
    15             pstmt.setInt(2, 1);
    16             pstmt.executeUpdate();
    17         }
    18         pstmt.close();
    19         // 关闭
    20         con.close();
    21         
    22     }
    View Code

    注意:xml的名字为:c3p0-config.xml,导入时从c3p0的包中拷贝到工程中。

  • 相关阅读:
    javascript命名规范
    angularjs指令参数transclude
    angular中的compile和link函数
    angularjs中的directive scope配置
    sublime text3同时编辑多行
    jquery中on/delegate的原理
    defered,promise回顾
    导航栏滚动到顶部后固定
    angularjs揭秘
    $stateParams
  • 原文地址:https://www.cnblogs.com/nicker/p/6820662.html
Copyright © 2011-2022 走看看