整理的c3p0工具类

为了以后方便使用特地对c3p0写了个帮助类,特记录在此:

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();
			}
		}
	}
}

 

你可能感兴趣的:(sql,mysql,jdbc)