C3P0 Connection Pool:
Set up connection pooling directly through Java code (you need to import C3P0 and database-connected jar packs in the engineering directory):
Step: 1. Create C3P0 Data source objects
2. Set the database connection basic information
3. Set database connection pool parameters
Import java.sql.Connection;
Import Com.mchange.v2.c3p0.ComboPooledDataSource; C3p0pool Connection Pool public class C3p0pool {public static void main (string[] args) throws Exception {//First step: Create C3P0 Data source Object Co
Mbopooleddatasource DataSource = new Combopooleddatasource ();
Step two: Set up database connection basic information/Set database to connect drive Datasource.setdriverclass ("Oracle.jdbc.OracleDriver");
Set the data connection URL datasource.setjdbcurl ("JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL");
Set up Database Connection account Datasource.setuser ("Xiaogang");
Set the password for the database connection account Datasource.setpassword ("Xiaogang");
Step three: Set the database connection pool parameters//Set the minimum number of connections in the database connection pool datasource.setminpoolsize (30);
Set the initial number of connections in the database connection pool datasource.setinitialpoolsize (30);
Set the maximum number of connections in the database connection pool datasource.setmaxpoolsize (50);
Not reached the maximum number of connections, the current need for new connection number set Datasource.setacquireincrement (10);
Long Start=system.currenttimemillis ();
for (int i=0;i<1000;i++) {Connection con=datasource.getconnection ();
Con.close ();
Long End=system.currenttimemillis (); System.out.println (End-start); }
}
Use the properties file to set the database connection basic information:
Step: 1. The configuration file is located in the SRC directory, and the profile path should follow the class loading method
InputStream in = null;
In=c3p0pool.class.getclassloader (). getResourceAsStream ("Com/xiaogang/jdbcc3p0.properties")
2. Associating a file with a Properties object
Properties Config = new properties ();
Config.load (in);
3. The Config.getproperty ("Driver") of the Config object can be used; get the properties in the file
Note:
Properties Property Information (property name and property value)
Properties prop = new properties ();
Stored data format: Property name = attribute value
Methods: Put (Attname,attvalue);
Prop.put ("name", "Xiaosan");
Prop.put ("Now", New Date ());
Gets the value of the property: String GetProperty (name)
String value=prop.getproperty ("name");
System.out.println ("Name:" +value);
Gets the value of the property: Object get (key)
String Nowvalue=prop.get ("Now"). ToString ();
Import java.beans.PropertyVetoException;
Import java.io.IOException;
Import java.sql.Connection;
Import java.sql.SQLException;
Import java.util.Properties;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0pool {private static Combopooleddatasource DataSource;
private static Properties Config = new properties (); private static void Init () {try {//Mount configuration file Config.load (C3P0Pool.class.getClassLoader (). getResourceAsStream ()
Com/heres/jdbcc3p0.properties "));
Declares the C3P0 data source Object DataSource = new Combopooleddatasource ();
Set the database connection driver Datasource.setdriverclass (config.getproperty ("Driver"));
Sets the data connection URL Datasource.setjdbcurl (config.getproperty ("url"));
Set up Database connection user account Datasource.setuser (config.getproperty ("user"));
Set the password for the database connection user account Datasource.setpassword (config.getproperty ("password"));
Set the number of initialization connection objects in the database connection pool datasource.setinitialpoolsize (30);
Set the minimum number of connection objects in the database connection pool datasource.setminpoolsize (30); Set the maximum number of connection objects in the database connection pool datasource.setmaxpoolsize (60);
When the connection is not enough, each new connection quantity datasource.setacquireincrement (10);
catch (IOException e) {e.printstacktrace ();
catch (Propertyvetoexception e) {e.printstacktrace ();
} public static void Main (string[] args) throws sqlexception{init ();
int i=1000;
Long begintime = System.currenttimemillis ();
while (i-->0) {Connection con = datasource.getconnection ();
Con.close ();
Long endtime = System.currenttimemillis ();
Long time = Endtime-begintime;
SYSTEM.OUT.PRINTLN ("Processing time:" +time); }
}
The corresponding properties file:
User=scott
Password=tiger
Driver=oracle.jdbc.driver.oracledriver
Url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
DBCP Connection pool (you need to import Commons-pool.jar, Commons-beanutils.jar, Commons-dbcp.jar, Commons-logging.jar, and database-connected jar packs in the engineering directory):
Import java.sql.Connection;
Import java.sql.SQLException;
Import java.util.Properties;
Import Javax.sql.DataSource;
Import Org.apache.commons.dbcp.BasicDataSourceFactory;
public class Dbcppool {private static DataSource datasource=null;
private static Properties Dbconfig = new properties (); public static void Init () {try {///Mount configuration file Dbconfig.load (DBCPPool.class.getClassLoader (). getResourceAsStream (
"Com/heres/jdbcdbcp.properties"));
System.out.println (DataSource);
Create the data source Object Datasource=basicdatasourcefactory.createdatasource (Dbconfig) through the factory class;
catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
} public static void Main (string[] args) throws sqlexception{init ();
int i=1000;
Long begintime = System.currenttimemillis ();
while (i-->0) {Connection con = datasource.getconnection ();
Con.close ();
Long endtime = System.currenttimemillis ();
Long time = Endtime-begintime; System.out.priNTLN ("Processing time:" +time); }
}
Corresponding configuration file:
Username=scott
Password=tiger
Driverclassname=oracle.jdbc.driver.oracledriver
Url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl