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 ...
随机推荐
- ABAP Development Tools的语法高亮实现原理
ABAP Development Tools的前端是Java,根本识别不了ABAP.那么在ADT里的ABAP语法高亮是如何实现的? 第一次打开一个report时,显示在ADT里的代码是没有任何语法高亮 ...
- JNI接口的使用(简单版)
详见 http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL2Qtc5q0tIQjG2yfwaU1MX4Ye
- UVA 11988 Broken Keyboard (链表)
简单题,题目要求显然是很多次插入,所以是链表. 插入两个语句,nxt[i] = nxt[u] 表示 i结点指向u的后继, nxt[u] = i表示把u的后继设成i. 设置一个头结点,指向一个不存在的结 ...
- k8s1.13.0二进制部署-node节点(四)
Master apiserver启用TLS认证后,Node节点kubelet组件想要加入集群,必须使用CA签发的有效证书才能与apiserver通信,当Node节点很多时,签署证书是一件很繁琐的事情, ...
- BCB:使用CppWebBrowser判断网页加载完成
void __fastcall TForm1::CppWebBrowser1DocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *U ...
- opensuse 15.0 安装ctdb
问题 1 2019/05/20 15:27:14.574363 ctdb-eventd[26329]: 60.nfs: /etc/ctdb/nfs-linux-kernel-callout: line ...
- oracle 将查询结果输出到txt文件里
在查询语句里先输入spool filepath 中间是需要查询的语句,最后spool off 就会把中间查询的结果都输入到file文件里 spool E:\log.txt; select id,nam ...
- 新环境安装 python3
参考 安装 python3 时,不要覆盖原环境的 python2.因为环境中有些程序是依赖 2 的,比如 yum.直接覆盖是会影响环境的. 最好的是编译安装 python3,执行指令是用 python ...
- centos6启动故障排除
centos6中boot文件被全部删除的故障排除 /boot文件里关于启动的核心文件有三个,/vmlinuz-2.6.32-696.e16.x86_64,initramfs-2.6.32-696.el ...
- python入门:if和else的基本用法
#!/usr/bin/env python # -*- coding:utf-8 -*- #2.X用raw_input,3.X用input #if和else的基本用法 name = input(&qu ...