数据库连接池
  数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。

  获取一个连接,系统要在背后做很多消耗资源的事情,大多时候,创建连接的时间比执行sql语句的时间还要长。
用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长。

  数据库连接池负责分配,管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个。

  c3p0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。c3p0一般是与Hibernate,Spring等框架一块使用的,当然也可以单独使用。
  dbcp没有自动回收空闲连接的功能,c3p0有自动回收空闲连接功能。

第一种方式:通过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/mydb?characterEncoding=GBK</property>
<property name="user" >root</property>
<property name="password" ></property> <property name="maxPoolSize" >15</property>
<property name="minPoolSize" >5</property>
<property name="initialPoolSize" >5</property>
</default-config>
</c3p0-config>

第二种方式:通过properties文件来配置

c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydb
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.user=root
c3p0.password= c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.maxPoolSize=20
c3p0.maxStatements=100
c3p0.minPoolSize=5

调用main函数来读取配置

public static void main(String[] args) throws Exception {

        ComboPooledDataSource cb = new ComboPooledDataSource();

        Connection conn=cb.getConnection();
System.out.println(conn.isClosed());
conn.close(); }

返回结果为false

通过下面的实验,来验证一下连接池的作用:

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Calendar; import com.mchange.v2.c3p0.ComboPooledDataSource; public class Test { public static void main1111(String[] args) throws Exception {
long start = Calendar.getInstance().getTimeInMillis();
for(int i=0; i<100;i++){
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
System.out.println(conn.isClosed());
conn.close();
}
long end = Calendar.getInstance().getTimeInMillis();
System.out.println(end-start);
}
public static void main(String[] args) throws Exception { long start = Calendar.getInstance().getTimeInMillis(); ComboPooledDataSource cb = new ComboPooledDataSource();
for(int i=0; i<100;i++){
Connection conn=cb.getConnection();
System.out.println(conn.isClosed());
conn.close();
}
long end = Calendar.getInstance().getTimeInMillis();
System.out.println(end-start);
} }

  main1111的函数是采用最基本的方式连接数据库,main函数是用连接池连接数据库,同样循环一百次第一个结果为8000毫秒,第二个约为1000毫秒。

  既然可以通过xml文件和properties文件来配置连接池,那么我们也可以通过别的方式进行配置:

(1)直接在main函数中定义

public static void main11(String[] args) throws Exception {
ComboPooledDataSource cd = new ComboPooledDataSource();
cd.setDriverClass("com.mysql.jdbc.Driver");
cd.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
cd.setUser("root");
cd.setPassword("");
cd.setMinPoolSize(5);
cd.setMaxPoolSize(10);
Connection conn = cd.getConnection();
System.out.println(conn.isClosed());
conn.close(); }

(2)通过读取properties配置采用${}方式在xml中读取

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK
user=root
password= minPoolSize=5
maxPoolSize=15
initialPoolSize=5
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:property-placeholder location="classpath:db.properties"/> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property> <property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
</bean> </beans>
public static void main3333(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
DataSource d = (DataSource)context.getBean("dataSource");
Connection conn = d.getConnection();
System.out.println(conn.isClosed());
conn.close();
}

Spring c3p0连接池配置的更多相关文章

  1. Spring之c3p0连接池配置和使用

    1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...

  2. day39-Spring 15-Spring的JDBC模板:C3P0连接池配置

    <!-- 配置C3P0连接池 --> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPo ...

  3. C3P0连接池配置(C3P0Utils.java)

    配置文件 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-confi ...

  4. (30)java web的hibernate使用-c3p0连接池配置

    hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...

  5. spring hibernate4 c3p0连接池配置

    c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...

  6. Spring c3p0连接池通过Hibernate配置

    首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html 然后调用这三个包. 配置hibernate.cfg.xml文件: &l ...

  7. C3p0连接池配置

    在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Connection co ...

  8. C3P0连接池配置方式

    c3p0的配置方式分为三种,分别是 1.setters一个个地设置各个配置项 2.类路径下提供一个c3p0.properties文件 3.类路径下提供一个c3p0-config.xml文件 1.set ...

  9. Spring c3p0连接池无法释放解决方案

    通过c3p0配置连接池的时候,在进行压力测试的时候,日志出现了这样一个错误:Data source rejected establishment of connection, message from ...

随机推荐

  1. pc端常见布局---水平居中布局 单元素不定宽度

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. cv2.minAreaRect() 生成最小外接矩形

    简介   使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是所要求最小外接矩形的点集数组或向量,这个点集不定个数.   cv2 ...

  3. js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了

    js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了

  4. How to restrict root user to access or modify a file and directory in Linux

    Now in this article I will show you steps to prevent or restrict access of root user to access certa ...

  5. linux - centos7 开放防火墙端口的新方式

    CentOS 升级到7之后,发现无法使用iptables控制Linuxs的端口, google之后发现Centos 7使用firewalld代替了原来的iptables. 下面记录如何使用firewa ...

  6. Bootstrap 标签

    本章将讲解bootstrap标签,标签可用于计数,提示和页面上其它的标记显示.使用class.laber来显示标签,如下面的实例所示 <!DOCTYPE html><html> ...

  7. Java String Integer转换 练习:编程求字符串“100”和“150”按十进制数值做差后的结果以字符串形式输出。

    package com.swift; public class String_To_Integer_Test { public static void main(String[] args) { /* ...

  8. 解决安装homebrew失败

    安装homebrew失败提示如下 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/maste ...

  9. VueJS坎坷之路222--vue cli 3.0引入静态文件

    前两天准备搭建一个vue小项目,当引入jquery脚本的时候一直找不到引入的文件: 在网上搜了好多vue添加静态文件的方法,发现大多数方法都是创建一个与文件夹src同等级的文件夹static存放引入的 ...

  10. 数据结构实用C语言基础

    大纲: 主要介绍了C语言中的指针,内存分配,两种传参方式,typedef的简单用法 关于C语言中的指针: 指针变量也称为指针(Pointer) 例如:int* p; 则p为一个指向int类型的指针. ...