Package com.c3p0.datapools;
Database Connection Pool Singleton mode
Import java.sql.Connection;
Import java.sql.SQLException;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
Import com.mchange.v2.c3p0.DataSources;
Public final class ConnectionManager {
private static ConnectionManager instance;
Private Combopooleddatasource DS;
Private ConnectionManager () throws Exception {
ds = new Combopooleddatasource ();
Ds.setdriverclass ("Oracle.jdbc.driver.OracleDriver");
Ds.setjdbcurl ("JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL");
Ds.setuser ("Scott");
Ds.setpassword ("Tiger");
//Initialize to get three connections, the value should be between Minpoolsize and Maxpoolsize. Default:3 initialpoolsize
Ds.setinitialpoolsize (3);
The maximum number of connections that are kept in the connection pool. Default:15 maxpoolsize
Ds.setmaxpoolsize (100);
The minimum number of connections that are kept in the connection pool.
Ds.setminpoolsize (1);
When the connection in the connection pool runs out, c3p0 the number of connections that are fetched at one time. Default:3 acquireincrement
Ds.setacquireincrement (10);
Check for idle connections in all connection pools every 60 seconds. default:0 Idleconnectiontestperiod
Ds.setidleconnectiontestperiod (60);
Maximum idle time, unused in 25,000 seconds, the connection is discarded. If 0, it will never be discarded. default:0 MaxIdleTime
Ds.setmaxidletime (25000);
All uncommitted operations are rolled back by default when the connection is closed. Default:false Autocommitonclose
Ds.setautocommitonclose (TRUE);
Defines test statements that are executed by all connection tests. This one significantly improves the test speed in the case of connection testing. Attention:
The test table must exist at the time of the initial data source. Default:null Preferredtestquery
Ds.setpreferredtestquery ("Select Sysdate from Dual");
Please use it only when you need it because of high performance consumption. If set to true then each connection commits the
Officer the validity of the test. It is recommended to use Idleconnectiontestperiod or automatictesttable
and other methods to improve the performance of the connection test. Default:false Testconnectiononcheckout
Ds.settestconnectiononcheckout (TRUE);
If set to true then the validity of the officer connection is obtained while the connection is made. Default:false Testconnectiononcheckin
Ds.settestconnectiononcheckin (TRUE);
Defines the number of repeated attempts to obtain a new connection from the database after a failure. Default:30 acquireretryattempts
Ds.setacquireretryattempts (30);
Interval in two connections, in milliseconds. default:1000 Acquireretrydelay
Ds.setacquireretrydelay (1000);
Getting a connection failure will cause any thread that waits for the connection pool to get the connection to throw an exception. But the data source is still valid
Reserved and continue trying to get the connection the next time you call Getconnection (). If set to true, then try to
When a connection failure is obtained, the data source declares that it has been fractured and closed permanently. Default:false breakafteracquirefailure
Ds.setbreakafteracquirefailure (TRUE);
<!--the time when the client calls getconnection () after the connection pool has run out, and then throws
SQLException, if set to 0, waits indefinitely. Unit milliseconds. Default:0--
<property name= "Checkouttimeout" >100</property>
<!--C3P0 will build a blank table named Test and test it with its own query statement. If this parameter is defined, then
Property Preferredtestquery will be ignored. You cannot do anything on this test sheet, it will be for C3P0 testing only
Use. Default:null-->
<property name= "Automatictesttable" >Test</property>
<!--the standard parameters of JDBC to control the number of preparedstatements loaded within the data source. But due to the statements of the pre-cache
belong to a single connection instead of the entire connection pool. So setting this parameter takes into account a variety of factors.
If both maxstatements and maxstatementsperconnection are 0, the cache is closed. Default:0-->
<property name= "Maxstatements" >100</property>
<!--maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. Default:0--
<property name= "Maxstatementsperconnection" ></property>
<!--C3P0 is asynchronous, and slow JDBC operations are done through the help process. Extending these operations can effectively improve performance
Multiple operations are performed at the same time through multithreading. Default:3-->
<property name= "Numhelperthreads" >3</property>
<!--the user to wait up to 300 seconds before modifying the system configuration parameters. DEFAULT:300--
<property name= "Propertycycle" >300</property>
}
public static final ConnectionManager getinstance () {
if (instance = = null) {
try {
Instance = new ConnectionManager ();
} catch (Exception e) {
E.printstacktrace ();
}
}
return instance;
}
Public synchronized final Connection getconnection () {
try {
return Ds.getconnection ();
} catch (SQLException e) {
E.printstacktrace ();
}
return null;
}
protected void Finalize () throws Throwable {
Datasources.destroy (DS); Close DataSource
Super.finalize ();
}
}