spring 使用c3po连接池
1 数据源:能够简单理解为数据的来源。
2 连接池:是缓存一定数量的数据库连接,当程序须要数据库连接的时候,直接在连接池中获取空暇的连接,使用完再放回连接池中,此连接又变成空暇状态,等待下一次连接。
有于开启连接和关闭连接比較耗费系统资源,有类连接池的管理能够降低这方面的开支。
3 常见连接池:c3p0,dbcp,proxool是常见开源的三种连接池。
Spring提供的DriverManagerDataSource总是新建一个连接,根本没有起到连接池的作用。
4 连接池获取连接的方法: Connection conn=dataSource.getConnection();
5 例如以下是数据源的使用:
c3p0:
UserDao:
package com.dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException; import javax.sql.DataSource; public class UserDao {
private DataSource dataSource; public DataSource getDataSource() {
return dataSource;
} public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} public void save(){
try {
Connection conn=dataSource.getConnection();
String sql="insert into t_student values(?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, "zhang");
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
UserService:
package com.service;
import com.dao.UserDao;
public class UserService {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.save();
}
}
applicationContext.xml:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
<property name="user" value="orcl"></property>
<property name="password" value="newsnews"></property>
</bean> <bean id="userDao" class="com.dao.UserDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userService" class="com.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
測试类:
package com.aop; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.UserService; public class Test { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService uc=(UserService) ac.getBean("userService");
uc.save();
} }
6 为了方便维护,改动数据源,能够把数据源的值写在配置中,这样的方式是通过设置站位符实现的。使用Spring的 PropertyPlaceholderConfigurer。
须要在spring配置中添加一个管理配置的bean,用来管理数据源取值的配置。
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:c3p0.properties</value>
</property>
</bean>
classpath:配置的路径。从src文件夹開始。
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.*" />
<context:annotation-config /> <bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:c3p0.properties</value>
</property>
</bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean> </beans>
c3p0.properties
driverClass=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost:1521:XE
user=orcl
password=newsnews
=前面的值要和${}里面的值相应。
spring 使用c3po连接池的更多相关文章
- JdbcTemplae使用入门&&Spring三种连接池配置&&Spring配置文件引用外部properties文件
JdbcTemplate的使用 Spring为了各种支持的持久化技术,都提供了简单操作的模版和回调. JdbcTemplate 简化 JDBC 操作HibernateTemplate 简化 Hiber ...
- 【转】SSH中 整合spring和proxool 连接池
[摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...
- HttpClient实战三:Spring整合HttpClient连接池
简介 在微服务架构或者REST API项目中,使用Spring管理Bean是很常见的,在项目中HttpClient使用的一种最常见方式就是:使用Spring容器XML配置方式代替Java编码方式进行H ...
- spring常用的连接池属性文件配置
(一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...
- Spring之c3p0连接池配置和使用
1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...
- Spring使用proxool连接池 管理数据源
一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控 ...
- spring配置druid连接池和监控数据库访问性能
Druid连接池及监控在spring配置如下: <bean id="dataSource" class="com.alibaba.druid.pool.DruidD ...
- (七)Spring 配置 c3p0 连接池
目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...
- spring hibernate4 c3p0连接池配置
c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...
随机推荐
- jsp出现错误can not find the tag directory /web-inf/tags
百度google了一大圈没找到中文答案,无奈之下硬着头皮看了一个英文答案http://stackoverflow.com/questions/11502703/eclipse-can-not-find ...
- A list is a sequence
Like a string, a list is a sequence of values. In a string, the values are characters; in a list, th ...
- 浅谈微信smali注入
作者:郭少雷 搞android搞了几年也没搞出个啥牛逼app出来,眼看时下最火的app微信如此火热,实在想搞搞它,索性就想着给它加点东西进去. 以下内容纯属本人个人爱好,仅限个人学习android用途 ...
- PostgreSQL相关总结
源码安装PostgreSQL总结 简明安装步骤(其中prefix指定PostgreSQL的安装目录,该目录与数据目录pgdata和PostgreSQL的源代码包目录均无关) yum -y instal ...
- Division Game UVA - 11859 Nim
Code: #include<cstdio> #include<algorithm> using namespace std; #define maxn 10005 int n ...
- NodeJS学习笔记 进阶 (1)Nodejs进阶:服务端字符编解码&乱码处理(ok)
个人总结:这篇文章主要讲解了Nodejs处理服务器乱码及编码的知识,读完这篇文章需要10分钟. 摘选自网络 写在前面 在web服务端开发中,字符的编解码几乎每天都要打交道.编解码一旦处理不当,就会出现 ...
- vue-cli生成的模板各个文件详解(转)
vue-cli脚手架中webpack配置基础文件详解 一.前言 原文:https://segmentfault.com/a/1190000014804826 vue-cli是构建vue单页应用的脚手架 ...
- [Bug]Python3.x SyntaxError: 'ascii' codec can't decode byte 0xe4 in position
安装arch后就没再用python了 昨天管服务器的大佬在跑贝叶斯分类器的时候发现正确率有问题 我赶紧去做优化,然后就有这样的报错 Python 3.6.4 (default, Jan 5 2018, ...
- python中的future,你见过可以使用未来版本模块的语言吗?
import xxx from yy.xxx import xx from yy.xxx import xx as x python最常见的导包导模块语句 yy为包名,包就是文件夹,模块就是xxx.p ...
- 【Educational Codeforces Round 37 B】 Tea Queue
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用一个队列来模拟排队就好. 队列放三元组(x,y,z) x表示人的下标,y和z分别表示进入和退出时间. 然后枚举时间从1到5000 ...