1 Data source: Can be easily understood as the source of the data.
2 Connection pool: is the cache a certain number of database connections, when the program needs a database connection, directly in the connection pool to get an idle connection, and then put back into the connection pool, this connection becomes idle, waiting for the next connection. There is a cost of system resources to open and close connections, and the management of a class connection pool can reduce this overhead.
3 Common Connection pools: C3p0,dbcp,proxool is a common open source three connection pool. Spring provides Drivermanagerdatasource always creates a new connection and does not play a role in connection pooling at all.
4 connection pooling method of obtaining connections: Connection conn=datasource.getconnection ();
5 The following is the use of the data source:
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);p S.setint (1, 1);p s.setstring (2, "Zhang");p s.executeupdate ();} catch (SQLException e) {//TODO auto-generated catch Blocke.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.XSDHTTP://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>
Test class:
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 ();}}
Spring uses C3PO connection pool