The jar package that needs to be prepared:
The MySQL jar package Mysql-connector-java-5.1.39-bin is currently 5.1.39 Version:
http://dev.mysql.com/downloads/connector/j/
C3P0 of 2 packages:
https://sourceforge.net/projects/c3p0/
C3p0-0.9.1.2.jar and C3p0-sources-0.9.1.2.jar
Configuration file: C3p0-config.xml
The name must be for this!
<c3p0-config> <!--default configuration, if not specified, use this configuration-- <default-config> < property name="Driverclass">Com.mysql.jdbc.Driver</Property > < property name="Jdbcurl"> <! [cdata[jdbc:mysql://127.0.0.1:3306/hncu?useunicode=true&characterencoding=utf-8]]> </Property > < property name="user">Root</Property > < property name="password">1234</Property > <!--Initialize Pool size -- < property name="Initialpoolsize">2</Property > <!--maximum idle time - < property name="MaxIdleTime">30</Property > <!--maximum number of connections -- < property name="Maxpoolsize">10</Property > <!--at least several connections -- < property name="Minpoolsize">2</Property > <!--How many batch statements can be executed at a time -- < property name="maxstatements">50</Property > </default-config> <!--named configuration-- <named-config name="Demo"> < property name="Driverclass">Com.mysql.jdbc.Driver</Property > < property name="Jdbcurl">Jdbc:mysql://127.0.0.1:3306/hncu</Property > < property name="user">Root</Property > < property name="password">1234</Property > < property name="Acquireincrement">5</Property ><!--If the pool does not have enough data to connect at a time < property name="Initialpoolsize">100</Property > < property name="Minpoolsize">50</Property > < property name="Maxpoolsize">1000</Property > < property name="maxstatements">0</Property > < property name="Maxstatementsperconnection">5</Property > <!--He ' s important, but there 's only one of him-- </named-config></c3p0-config>
Demo Code:
PackageCN.HNCU.C3P0;ImportJava.beans.PropertyVetoException;ImportJava.sql.Connection;ImportJava.sql.SQLException;ImportOrg.junit.Test;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public class c3p0demo { @Test //Pure Java method using C3P0 Public void C3p0demo()throwsPropertyvetoexception, SQLException {Combopooleddatasource pool =NewCombopooleddatasource (); Pool.setuser ("Root");//user namePool.setpassword ("1234");//user passwordPool.setjdbcurl ("Jdbc:mysql://127.0.0.1:3306/hncu?useunicode=true&characterencoding=utf-8");//MySQL database connection URLPool.setdriverclass ("Com.mysql.jdbc.Driver");//pool can call the set method to configure the connection pool After the connection is closed, the memory is freed and the next fetch is reopened (the memory address is not shared) for(inti =0; I < -; i++) {Connection con = pool.getconnection (); SYSTEM.OUT.PRINTLN (i +":"+ con);ifI2==0) {con.close (); } } }@Test //demo using C3P0 with configuration file Public void C3p0propertydemo()throwsSQLException {Combopooleddatasource pool =NewCombopooleddatasource ();//null parameter, automatically load the "C3p0-config.xml" profile under the Classpath directory the storage location and name of the profile---configuration file must be, and use the "Default configuration" //combopooleddatasource pool = new Combopooleddatasource ("demo");//Load "demo" as defined in "C3p0-config.xml" file, this configuration element for(intI=0;i< -; i++) {Connection con = pool.getconnection (); System.out.println (i+":"+ Con.hashcode ()); } }}
Wrap up the C3P0 connection pool.
PackageCN.HNCU.C3P0;ImportJava.sql.Connection;ImportJava.sql.SQLException;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public class c3p0pool { //Our packing, just to make the c3p0 pool make each thread (client) Get the same connection, convenient to do business under the B/S framework Private StaticDataSource Pool;Private StaticThreadlocal<connection> T =NewThreadlocal<connection> ();Static{pool =NewCombopooleddatasource ("Demo");//The parameter here depends on your configuration file, you can not write, with the default configuration} Public StaticDataSourceGetdatasource(){returnPool } Public StaticConnectiongetconnection()throwssqlexception{Connection con = t.get ();if(con==NULL) {con=pool.getconnection (); T.set (con); }returnCon }}
Reprint please attach the original blog link:
http://blog.csdn.net/qq_26525215
MySQL---Database from getting started to the Great God Series (14)-combopooleddatasource (c3p0 connection pool configuration)