题目

  • C3p0
开源的,成熟的,高并发第三方数据库连接池,作者是 Steve Waldman,相关的文档资料比较完善。
大名鼎鼎的hibernate框架就使用了c3p0数据库连接池。
官方主页:http://www.mchange.com/projects/c3p0/index.html
  • Druid
作者是阿里巴巴的wenshao,号称是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。
官方主页:https://github.com/alibaba/druid

代码

C3p0

下载后将项目两个jar包拷贝到项目中
 c3p0-0.9.5.2.jar
 mchange-commons-java-0.2.11.jar

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://localhost:3306/test-pool</property>
  <property name="user">root</property>
  <property name="password">monkey1024</property>
  <!-- 初始化连接的数量 -->
  <property name="initialPoolSize">10</property>
  <!-- 最大空闲时间,单位是秒 -->
  <property name="maxIdleTime">30</property>
  <!-- 池中最大连接的数量 -->
  <property name="maxPoolSize">100</property>
  <!-- 池中最小连接的数量 -->
  <property name="minPoolSize">10</property>
</default-config>
</c3p0-config>

t_user/test-pool数据库表

CREATE TABLE `t_user` (
`id` varchar(10) NOT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

C3P0Util工具类

package com.monkey1024.jdbc.util;

import java.sql.Connection;
import java.sql.SQLException;
  
import com.mchange.v2.c3p0.ComboPooledDataSource;
  
public class C3P0Util {
  // 得到一个c3p0的数据源
  private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

  // 从数据源中得到一个连接对象
  // 这个返回的connection实际上是c3p0经过装饰之后的connection
  public static Connection getConnection() {
      try {
          return dataSource.getConnection();
      } catch (SQLException e) {
          throw new RuntimeException("服务器错误");
      }
  }

  //查看连接池的状态
  public static void poolStatus() {
      try {
          System.out.println("清闲的:" + dataSource.getNumIdleConnections());
          System.out.println("忙碌的:" + dataSource.getNumBusyConnections());
          System.out.println("所有的:" + dataSource.getNumConnections());
      } catch (SQLException e) {
          e.printStackTrace();
      }

  }

}

测试类C3P0Test

package com.monkey1024.jdbc.test;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.monkey1024.jdbc.util.C3P0Util;

public class C3P0Test {

  public static void main(String[] args) {
      insert();
      try {
          //睡眠一下方便查看状态
          Thread.sleep(100);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      C3P0Util.poolStatus();
  }

  public static void insert() {

      String sql = "insert into t_user(id,name) values('1030','jack')";
      try (Connection conn = C3P0Util.getConnection(); 
              PreparedStatement ps = conn.prepareStatement(sql)) {
          //查看连接池的状态
          C3P0Util.poolStatus();
          ps.executeUpdate();
      } catch (Exception e) {
          e.printStackTrace();
      }

  }

}

Druid

下载相关jar包:druid-1.1.8.jar

dbconfig.properties

url=jdbc:mysql://localhost:3306/test-pool
username=root
password=monkey1024
#初始化的连接个数
initialSize=10
#最大连接个数
maxActive=20
#最小连接个数
minIdle=10

DruidUtil工具类

package com.monkey1024.jdbc.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
 
public class DruidUtil {
  // 得到一个Druid的数据源
  private static DruidDataSource dataSource = null;
  
  static{
      Properties properties = new Properties();
      try {
          //加载配置文件
          //properties.load(new FileInputStream("build/classes/dbconfig.properties"));
          //下面这种写法会从classpath下来查找配置文件
          properties.load(DruidUtil.class.getClassLoader().getResourceAsStream("dbconfig.properties"));
          //得到一个数据源 
          dataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(properties);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
 
 
  // 从数据源中得到一个连接对象
  // 这个返回的connection实际上是Druid经过装饰之后的connection
  public static Connection getConnection() {
      try {
          return dataSource.getConnection();
      } catch (SQLException e) {
          throw new RuntimeException("服务器错误");
      }
  }
}

测试类DruidTest

package com.monkey1024.jdbc.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.monkey1024.jdbc.util.DruidUtil;
public class DruidTest {
  public static void main(String[] args) {
      insert();
      try {
          //睡眠一下方便查看状态
          Thread.sleep(100);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  public static void insert() {
      String sql = "insert into t_user(id,name) values('12012','jack')";
      try (Connection conn = DruidUtil.getConnection(); 
              PreparedStatement ps = conn.prepareStatement(sql)) {
          ps.executeUpdate();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}