Spring c3p0连接池配置
数据库连接池
数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。
获取一个连接,系统要在背后做很多消耗资源的事情,大多时候,创建连接的时间比执行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连接池配置的更多相关文章
- Spring之c3p0连接池配置和使用
1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...
- day39-Spring 15-Spring的JDBC模板:C3P0连接池配置
<!-- 配置C3P0连接池 --> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPo ...
- C3P0连接池配置(C3P0Utils.java)
配置文件 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-confi ...
- (30)java web的hibernate使用-c3p0连接池配置
hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...
- spring hibernate4 c3p0连接池配置
c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...
- Spring c3p0连接池通过Hibernate配置
首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html 然后调用这三个包. 配置hibernate.cfg.xml文件: &l ...
- C3p0连接池配置
在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ②连接数据库(Connection co ...
- C3P0连接池配置方式
c3p0的配置方式分为三种,分别是 1.setters一个个地设置各个配置项 2.类路径下提供一个c3p0.properties文件 3.类路径下提供一个c3p0-config.xml文件 1.set ...
- Spring c3p0连接池无法释放解决方案
通过c3p0配置连接池的时候,在进行压力测试的时候,日志出现了这样一个错误:Data source rejected establishment of connection, message from ...
随机推荐
- userBean-作用范围application
package com.java1234.model; public class Student { private String name;private int age; public Strin ...
- 洛谷 P2733 家的范围 Home on the Range
题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形的牧场上吃草.)遗憾的是,他的奶牛已经毁坏一些土地.( 一 ...
- windows 密钥
server 2016数据中心CB7KF-BWN84-R7R2Y-793K2-8XDDG
- Android(java)学习笔记114:Service生命周期
1.Service的生命周期 Android中的Service(服务)与Activity不同,它是不能和用户交互,不能自己启动的,运行在后台的程序,如果我们退出应用的时候,Servic ...
- Kubernetes之pod的属性
属性名称 取值类型 是否必选 取值说明 version String Required(必) 版本号,例如v1 kind String Required pod m ...
- 为DataGridView控件实现复选功能
实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...
- echarts实现仪表盘(自己动起来,没有后端,顺便重温math.random
let a = parseInt(Math.random() * (2 + 1), 10); let arr = []; arr.push(res[a]); let option = { toolti ...
- 《毛毛虫组》【Alpha】Scrum meeting 5
第二天 日期:2019/6/18 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: 出入库货物年统计模块设计及系统的测试运行: (1)对数据库表--tb_InStore和tb_OutSto ...
- hash join
hash join是oracle里面一个非常强悍的功能,当做hash join时,oracle会选择一个表作为驱动表,先根据过滤条件排除不必要的数据,然后将结果集做成hash表,放入进程的hash a ...
- c++作业:输入两个整数,用函数求两数之和。函数外部声明有什么作用?
#include <iostream> using namespace std; int main(){ //求两数的和? int a,b,s; cout<<"请你输 ...