zoukankan      html  css  js  c++  java
  • 数据库连接池

    1.概念:其实就是一个容器,存放数据库连接的容器
      当系统初始化好了之后,容器就被创建,容器中会申请一些连接对象,当用户访问的时候就可以从容器中获取连接对象

    2.好处:
      1.节约资源
      2.用户访问高效

    3.实现:
      1.标准接口:DataSource javax.sql包下的
        1.方法:
         1.1 获取连接:getConnection();
         1.2归还连接:connection.close();前提是从连接池里面获取的。

      2.一般我们不会自己去实现他,由数据库产商实现
        2.1 C3P0 :数据库连接技术
        2.2Druid:数据库连接技术,由阿里巴巴提供

    4.C3P0:使用
      步骤:
        1.导入jar包(两个)c3p0-09.5.2.jar mchange-commons -java-0.2.12.jar
          (不能忘记同时需要导入数据库驱动包)

        2.定义配置文件:  
          2.1名称:c3p0.properties 或者c3po-config.xml(名称定义规范化)

    <c3p0-config>
      <!-- 使用默认的配置读取连接池对象 -->
      <default-config>
          <!--  连接参数 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
        <property name="user">root</property>
        <property name="password">root</property>
        
        <!-- 连接池参数 -->
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">10</property>
        <property name="checkoutTimeout">3000</property>
      </default-config>
    
      <named-config name="otherc3p0"> 
        <!--  连接参数 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
        <property name="user">root</property>
        <property name="password">root</property>
        
        <!-- 连接池参数 -->
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">8</property>
        <property name="checkoutTimeout">1000</property>
      </named-config>
    </c3p0-config>

          2.2路径:直接将文件放在src目录下面就好

        3.创建核心对象 数据库连接池对象ComboPooledDataSource

        4.获取连接:getconnection

    public class C3P0Demo01 {
        public static void main(String[] args) throws SQLException {
            //获取连接对象
            DataSource ds = new ComboPooledDataSource();
            Connection conn = ds.getConnection();
            System.out.println(conn);
        }
    }

    5.Druid:数据库连接池技术,阿里巴巴贡献

      步骤:
        1.导入jar包:druid-1.0.9.jar 
        2.定义配置文件:  可以是properties形式,也可以是容易的名称,放在任意的位置下(侧面说明需要加载配置文件);

        3.加载配置文件,Properties
        4.获取数据库连接池对象:通过工厂来获取:DruidDataSourceFactory
        5.获取连接:getConnection

    public class DruidDemo01 {
        public static void main(String[] args) throws Exception {
            //1.导入jar包
            //2.定义配置文件
            //3.加载配置文件
            Properties pro = new Properties();
    
            InputStream is = DruidDemo01.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);
            //4.获取连接池对象
            DataSource ds = DruidDataSourceFactory.createDataSource(pro);
            //5.获取连接
            Connection conn = ds.getConnection();
            System.out.println(conn);
        }
    }
    如有错误,望费心指出。 感激涕零。
  • 相关阅读:
    Jupyter notebook添加Anaconda中的虚拟环境
    Django踩坑之在Django中创建项目时ImportError: No module named django.core
    Django踩坑之ExtendsNode: extends 'base/base.html'> must be the first tag in the template.
    git 中文文件名乱码
    Django开发过程中遇到的问题和解决方案
    Advanced pandas
    Pycomcad中的过滤机制及访问引用块内对象简述
    头大的一篇日志 细节总结 唐诗三百首
    UIBarItem 的设置
    MJExtension json快速解析
  • 原文地址:https://www.cnblogs.com/zouxiaopq/p/12568752.html
Copyright © 2011-2022 走看看