整理されたc 3 p 0ツールクラス
後で使いやすいようにc 3 p 0にヘルプクラスを書いたので、ここに記録します.
package db.c3p0;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* c3p0
* * :
* driveClassName:JDBC ;
* maxActive: ;
* minActive: ;
* initialPoolSize: ;
* maxWait: , ;
* password: ;
* url: JDBC URL ;
* user: ;
* maxIdleTime: , 。 0 ;
* acquireIncrement: c3p0 ;
* idleConnectionTestPeriod: ;
* acquireRetryAttempts: ;
* testConnectionOnCheckout: 。 true connection
。 idleConnectionTestPeriod automaticTestTable
;
* breakAfterAcquireFailure: 。
, getConnection() 。 true,
。;
* maxStatements:JDBC , PreparedStatements 。 statements
connection 。 。
maxStatements maxStatementsPerConnection 0, 。;
* @author dxz
*
*/
public class DBHelp {
private static ComboPooledDataSource dataSource = null;
private static String username = "jspcms";
private static String password = "jspcms";
private static String url = "jdbc:mysql://localhost:3306/jspcms";
private static String drive = "com.mysql.jdbc.Driver";
private static int maxActive = 30;
private static int minActive = 2;
private static int initialPoolSize = 2;
private static int maxIdleTime = 60;
private static int acquireIncrement = 5;
private static int maxStatements = 0;
private static int idleConnectionTestPeriod = 60;
private static int acquireRetryAttempts = 30;
private static boolean breakAfterAcquireFailure = true;
private static boolean testConnectionOnCheckout = false;
private Connection conn = null;
/**
*
* @throws Exception
*/
private void init() throws Exception{
if(dataSource == null){
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(this.drive);
dataSource.setJdbcUrl(this.url);
dataSource.setUser(this.username);
dataSource.setPassword(this.password);
dataSource.setMaxPoolSize(this.maxActive);
dataSource.setMinPoolSize(this.minActive);
dataSource.setInitialPoolSize(this.initialPoolSize);
dataSource.setMaxIdleTime(this.maxIdleTime);
dataSource.setAcquireIncrement(this.acquireIncrement);
dataSource.setMaxStatements(this.maxStatements);
dataSource.setIdleConnectionTestPeriod(this.idleConnectionTestPeriod);
dataSource.setAcquireRetryAttempts(this.acquireRetryAttempts);
dataSource.setBreakAfterAcquireFailure(this.breakAfterAcquireFailure);
dataSource.setTestConnectionOnCheckout(this.testConnectionOnCheckout);
}
}
/**
*
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception{
try {
if(dataSource == null){
this.init();
}
this.conn = (Connection) dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw e;
}
return conn;
}
/**
*
*/
public void closeConnection(){
if(this.conn != null){
try {
this.conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}