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连接池的更多相关文章

  1. JdbcTemplae使用入门&&Spring三种连接池配置&&Spring配置文件引用外部properties文件

    JdbcTemplate的使用 Spring为了各种支持的持久化技术,都提供了简单操作的模版和回调. JdbcTemplate 简化 JDBC 操作HibernateTemplate 简化 Hiber ...

  2. 【转】SSH中 整合spring和proxool 连接池

    [摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...

  3. HttpClient实战三:Spring整合HttpClient连接池

    简介 在微服务架构或者REST API项目中,使用Spring管理Bean是很常见的,在项目中HttpClient使用的一种最常见方式就是:使用Spring容器XML配置方式代替Java编码方式进行H ...

  4. spring常用的连接池属性文件配置

    (一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...

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

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

  6. Spring使用proxool连接池 管理数据源

    一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控 ...

  7. spring配置druid连接池和监控数据库访问性能

    Druid连接池及监控在spring配置如下: <bean id="dataSource" class="com.alibaba.druid.pool.DruidD ...

  8. (七)Spring 配置 c3p0 连接池

    目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...

  9. spring hibernate4 c3p0连接池配置

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

随机推荐

  1. SDUTOJ 2776 小P的故事——奇妙的分组

    #include<iostream> #include<math.h> #include<memory.h> using namespace std; int dp ...

  2. 7.stack

    #include <iostream> #include <stack> #include <algorithm> #include <list> #i ...

  3. VS导出方法名和方法备注的方法

    VS导出方法名和方法备注的方法 方法一: 只能导出图片格式的UML 类图 1.点击查看类图 2.在空白处点击讲关系导出为图像 方法二: 是把整个类库的方法名都罗列出来 这个方便整理一些 具体方法如下 ...

  4. 机器学习(七) PCA与梯度上升法 (下)

    五.高维数据映射为低维数据 换一个坐标轴.在新的坐标轴里面表示原来高维的数据. 低维 反向 映射为高维数据 PCA.py import numpy as np class PCA: def __ini ...

  5. CheckException和RuntimeException

    java文档中对RuntimeException的定义是: RuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类. 可能在执行方法期间抛出但未被捕获的 Runt ...

  6. ES6中常用的简写方式

    1. var foo = 'bar'; var baz = {foo}; baz // {foo: "bar"} // 等同于 var baz = {foo: foo}; 2. f ...

  7. UVa 1151 Buy or Build【最小生成树】

    题意:给出n个点的坐标,现在需要让这n个点连通,可以直接在点与点之间连边,花费为两点之间欧几里得距离的平方,也可以选购套餐,套餐中所含的点是相互连通的 问最少的花费 首先想kruskal算法中,被加入 ...

  8. Tomcat IO阻塞异常

    tomcat的maxThreads.acceptCount(最大线程数.最大排队数) tomcat 的Connector配置如下 <Connector port="8080" ...

  9. centOS7下 安装nodejs+nginx+mongodb+pm2部署vue项目

    一.购买服务器并远程连接 1.购买服务器和域名 可以选择阿里云或者是其他的厂商的服务器.然后会获得服务器ip地址,用户名和密码. 购买域名,将域名绑定到ip地址上. 2.下载xshell,winscp ...

  10. 用Electron开发企业网盘(二)--分片下载

    书接上文,背景见:https://www.cnblogs.com/shawnyung/p/10060119.html HTTP请求头  Range 请求资源的部分内容(不包括响应头的大小),单位是by ...