1.c3p0 use of data sources
C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. C3P0 has its own format file, as follows
<?xml version= "1.0" encoding= "UTF-8"?> <c3p0-config> <default-config> <property name= "Driverclass" >com.mysql.jdbc.Driver</property> <property name= "Jdbcurl" >jdbc:mysql:///day05</property> <property name= "user" >root</property> <property name= "password" >root</property> <property name= "initialpoolsize" >10</property> <property name= "MaxIdleTime" >30</property> <property name= "maxpoolsize" >100</property> <property name= "minpoolsize" >10</property> </default-config> </c3p0-config> |
- Guide Jar Package:
- Establishing a C3P0 connection
Package Com.beiwo.day05.util;import Java.sql.connection;import Java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;import Javax.sql.datasource;import Com.mchange.v2.c3p0.combopooleddatasource;public Class C3p0util {//c3p0 data source private static Combopooleddatasource datasource=new Combopooleddatasource ();p ublic static DataSource Getdatasource () {return DataSource;} public static Connection Getconn () {try {return datasource.getconnection ();} catch (SQLException e) {throw new Runtimeexc Eption (e);}} public static Connection GetConn2 () {Connection conn=null;try {conn=datasource.getconnection ();} catch (Exception e) { E.printstacktrace ();} Return conn;} Release the connection public static void Realease (ResultSet rs,statement stmt,connection conn) {try {if (null!=rs) {Rs.close ()}} catch ( Exception e) {//Todo:handle Exception}try {if (null!=stmt) {stmt.close ();}} catch (Exception e) {//Todo:handle Exception }try {if (null!=conn) {conn.close ();}} catch (Exception e) {//Todo:handle Exception}}}
The use of test c3p0
Package Com.beiwo.day05.test;import Java.sql.connection;import Org.junit.test;import com.beiwo.day05.util.C3P0Util ;p Ublic class Testc3p0 {@Testpublic void testc3p0 () {Connection Connection = C3p0util.getconn (); System.out.println (Connection.getclass (). GetName ()); C3p0util.realease (NULL, NULL, connection);}}
C3P0 use of data sources