zoukankan      html  css  js  c++  java
  • 数据库连接池(c3p0与druid)

    1.数据库连接池概念

    其实就是一个容器(集合),存放数据库连接的容器。当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。可以节约资源,用户访问也能高效一点。

    2.实现数据库连接池

    要创建数据库连接池要实现 java.sql包下的 DataSource 接口。此接口有一个方法 getConnection() 可以获得数据库连接对象。

    一般这个接口不由我们去实现,有数据库厂商来实现:

    常见有 C3P0 和 druid 。

    其中druid是由阿里巴巴提供的。

    3.C3P0数据库连接池技术

    (1)导入jar包 (两个) c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar。当然还有jdbc的包

    (2)定义配置文件

    名称: c3p0.properties 或者 c3p0-config.xml(固定的,不能变)
    路径:直接将文件放在src目录下。(固定,不能变)

    配置文件样本:

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

    (3)创建数据库连接池对象 ComboPooledDataSource

    (4)获取连接: getConnection

    示例:

    public class Demo1 {
        public static void main(String[] args) throws SQLException {
            ComboPooledDataSource cpd = new ComboPooledDataSource();  // 另一个构造方法可以加参数,表示用配置文件的<named-config></named-config>里面的配置
            Connection connection = cpd.getConnection();
            System.out.println(connection);
        }
    }

    4.Druid数据库连接技术

    (1)导入jar包 druid-1.0.9.jar

    (2)定义配置文件:是properties形式的,可以叫任意名称,可以放在任意目录下

    (3)加载配置文件。Properties

    (4)获取数据库连接池对象:通过工厂来来获取  DruidDataSourceFactory

    (5)获取连接:getConnection

    示例:

    配置文件:

    driverClassName=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/test
    username=root
    password=xxx
    initialSize=5
    maxActive=10
    maxWait=3000

    代码:

    public class Demo2 {
        public static void main(String[] args) throws Exception {
            // 加载配置文件
            Properties properties = new Properties();
            InputStream is = Demo2.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(is);
    
            //获取连接池对象
            DataSource ds = DruidDataSourceFactory.createDataSource(properties);
    
            // 获取连接
            Connection connection = ds.getConnection();
    
            System.out.println(connection);
        }
    }
  • 相关阅读:
    没有什么,开发ASP.NET时随便写写,想到什么写什么
    MS SQL Server带有时间的记录怎样查询
    给RadioButtonList绑定Selected的值
    在GridView控件内文本框实现TextChanged事件
    MS SQL Server递归查询
    ASP.NET MVC使用jQuery无刷新上传
    MySQL 慢查询操作梳理
    ubuntu系统下防火墙简单使用
    crontab日常使用梳理
    ubuntu下nginx+php5的部署
  • 原文地址:https://www.cnblogs.com/chichung/p/10316985.html
Copyright © 2011-2022 走看看