Spring uses C3PO connection pool

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.