1. 程式人生 > >spring 使用c3po連接池

spring 使用c3po連接池

package 路徑 not com .com 連接池 nts mchange base


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連接池