C3P0 examples and parameters detailed

Source: Internet
Author: User
Tags time interval

You need to import C3p0.jar to execute, and you need to import the appropriate MySQL database driver package.

Package com.yjpeng.c3p0;

Import java.beans.PropertyVetoException;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.Reader;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;

Import Javax.sql.ConnectionPoolDataSource;

Import Com.mchange.v2.c3p0.ComboPooledDataSource;
Import Com.mchange.v2.c3p0.impl.DefaultConnectionTester;

Public final class ConnectionManager {
Private Combopooleddatasource DS;
Private ConnectionManager Connmanger;
Private ConnectionManager () throws propertyvetoexception{
ds = new Combopooleddatasource ();
Set up a database-driven class for the C3P0 connection pool
Ds.setdriverclass ("Com.mysql.jdbc.Driver");
To set the C3P0 connection pool database connection URL
Ds.setjdbcurl ("Jdbc:mysql://127.0.0.1:3306/school");
Set C3P0 connection Pool database user name
Ds.setuser ("root");
Set the C3P0 connection pool database password
Ds.setpassword ("root");
The number of C3P0 to create a new connection when the connection in the connection pool is exhausted 2
Ds.setacquireincrement (2);
Acquireretryattempts: Defines the number of repeated attempts to get a new connection from the database after it has failed, by default of 30 seconds
Ds.setacquireretryattempts (30);
Two time interval in the connection, the unit millisecond, default is 1000;
Ds.setacquireretrydelay (3000);
By default, all uncommitted operations are rolled back when the connection is closed. Default to FALSE;
Ds.setautocommitonclose (FALSE);
/**
* C3P0 will build an empty table named Test and test it with its own query statement. If this parameter is defined, then the property preferredtestquery is ignored.
* You can't do anything on this test table, it will be used for C3P0 test, default is null;
*/
Ds.setautomatictesttable ("Teacher");
/**
* Getting a connection failure will cause all threads waiting to get the connection to throw an exception. However, the data source remains valid,
* and continue trying to get the connection the next time you call Getconnection (). If set to True,
* Then the data source will be declared disconnected and closed permanently after attempting to acquire the connection. Default to FALSE;
*/
Ds.setbreakafteracquirefailure (FALSE);
/**
* When the client invokes getconnection () After the connection pool is exhausted, waits for the new connection to be fetched, and the timeout is thrown sqlexception.
* If set to 0, wait indefinitely. Unit milliseconds, default to 0;
*/
Ds.setcheckouttimeout (1000);
/**
* Test the connection by implementing Connectiontester or Queryconnectiontester classes, the class name needs to be set to the fully qualified name.
* default is Com.mchange.v2.C3P0.impl.DefaultConnectionTester;
*/
Ds.setconnectiontesterclassname (New Defaultconnectiontester (). GetClass (). GetName ());
Hook method, in the operation of the relevant resources, "the connection he operates is a real database connection, not a proxy connection '
Ds.setconnectioncustomizerclassname ("");
Connection Pool Data source
Ds.setconnectionpooldatasource (ConnectionPoolDataSource object);
Idleconnectiontestperiod: Check the idle connections in all connection pools by the number of seconds, default 0 means do not check
Ds.setidleconnectiontestperiod (10);
The number of connections created when initializing, should be evaluated between minpoolsize and Maxpoolsize. Default is 3;
Ds.setinitialpoolsize (5);
Maximum idle time, a connection that exceeds the idle time will be discarded. 0 or negative numbers will never be discarded. Default is 0;
Ds.setmaxidletime (3);
The maximum number of connections reserved in the connection pool. Default is 15;
Ds.setmaxpoolsize (100);
/**
* JDBC Standard parameters to control the number of PreparedStatement loaded in the data source.
* But because the cached statement belong to a single connection rather than the entire connection pool.
* So setting this parameter takes into account a variety of factors, if maxstatements and Maxstatementsperconnection are both 0,
* The cache is closed. Default is 0;
*/
Ds.setmaxstatements (1000);
The maximum number of cached statement that a single connection has within the connection pool. Default is 0;
Ds.setmaxstatementsperconnection (100);
C3P0 are asynchronous operations, and slow JDBC operations are accomplished through the help process. Extending these operations can effectively improve performance, and multiple operations through multithreading are implemented simultaneously. Default is 3;
Ds.setnumhelperthreads (5);
/**
*preferredtestquery: Defines the test statements that are executed by all connection tests.
* This parameter can significantly improve the test speed when using the connection test. The test table must exist at the initial data source. default is null;
*propertycycle: The maximum number of seconds to wait before the user modifies system configuration parameters. Default is 300;
*testconnectiononcheckout: Because of the high performance, please use it only when you need it.
* If set to true then the validity is officers transferred Guevara for each connection submission.
* Recommended use of idleconnectiontestperiod or automatictesttable
* and other methods to improve the performance of the connection test. Default to FALSE;
*testconnectiononcheckin: If set to true then officers transferred Guevara the validity of the connection while obtaining the connection. The default is False.
*
*/
}

public static void Main (string[] args) {
try {
ConnectionManager Connmanger = new ConnectionManager ();
Connection conn = ConnManger.ds.getConnection ();
Statement st = Conn.createstatement ();
String sql = "SELECT * from teacher";
ResultSet rs = st.executequery (SQL);
while (Rs.next ()) {
System.out.println (Rs.getint ("id") + "," + rs.getstring ("name"));
}
catch (Propertyvetoexception e) {
E.printstacktrace ();
catch (SQLException e) {
E.printstacktrace ();
}

}
}

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.