⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcpool.java

📁 导出ORACLE数据库对象DDL语句的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.icbcsdc.ddlexp.pub.connectionpool;

import java.util.*;
import java.sql.*;

import com.icbcsdc.ddlexp.pub.staticLog.Logger;
import com.icbcsdc.ddlexp.pub.util.ExceptionHandle;


/**
 * @author zhangyc
 * 连接池实例类,用于根据给定的驱动名称、url等信息创建连接池实例。
 * 负责一组相关连接的管理
 */
public class JDBCPool {

	/**
	 * JDBC Pool名称
	 * */
	private String poolName;
	
   /**
    * JDBC driver类型
    */
   private String driver;
   
   /**
    * jdbc连接字符串
    */
   private String url;
   
   /**
    * 数据库用户名
    * */
   private String dbUser;
   
   /**
    * 数据库口令
    * */
   private String dbPassword;
   
   /**
    * 备份数据库的操作系统口令
    * */
   //private String osUser;
   
   /**
    * 连接池的最大值
    * */
   private int poolMaxSize=10;
   
   /**
    * 连接池的最小值
    * */
   private int poolMinSize=1;
   
   /**
    * 连接池每次增加的连接个数
    * */
   private int increaseStep=1;
   
   /**
    * 释放空闲连接的扫描次数
    * */
   private int scanCount=5;
   
   /**
    * 释放空闲连接的扫描时间间隔
    * */
   private int scanInterval=60000;
   
   /**
    * 备份目录
    * */
   private String backDir="/usr/local/mysql";
   
  
   /**
    * 由一组jdbc连接组成的连接池
    */
   private List connPool;
   
   /**
    * 标示空闲连接的位置
    * */
  // private int pos=0;
   
   /**
    * 释放空闲连接的已经扫描次数
    * */
   private int scanPos=0;
   
   /**
    * 定时器,用于启动定期扫描空闲连接的任务
    * */
   private Timer freeScan=new Timer(true);

   /**
    * 连接池启动后,等待DELAY时间,开始扫描空闲连接
    * */
   private static int DELAY=1000;   
   
   /**
    * 连接池是否正在关闭
    * */
   private boolean isShutting=false;
   
   /**
    * 标示连接状态的数组
   private boolean[] isUsing;

	/**
	 * 强制关闭连接池
	 * */   
   public static final int ABORT=1;
   
   /**
    * 等当前正在执行的事务完成后,关闭连接池
    * */
   public static final int TRANSACTIONAL=2;
   
   /**
    * 构造函数,根据参数给定的属性创建连接池
    * @param	driver	JDBC driver类型
    * @param	url		jdbc连接字符串。
    * @param	user	连接数据库时使用的用户名。
    * @param	password	连接数据库时使用的口令。
    * @param	poolMaxSize		连接池大小的上界。
    * @param	poolMinSize		连接池大小的下界。
    * @param	increaseStep	连接增加速度。
    * @param	scanInterval	扫描时间间隔。
    * @param	scanCount	扫描次数。
    */
   public JDBCPool(String poolName,String driver, String url,String user,String password,String backDir,int poolMinSize,int poolMaxSize,int increaseStep,int scanCount,int scanInterval) 
	throws JDBCException   
	{
    	Connection conn;

		if (poolName==null||poolName.trim().equals("")) {
			Logger.log(Logger.WARN, JDBCCnnMgrConstants.NULL_POOLNAME);
			//throw new Exception(JDBCCnnMgrConstants.NULL_POOLNAME);
		}
		if (driver==null||driver.trim().equals("")) {
			Logger.log(Logger.WARN, JDBCCnnMgrConstants.NULL_DRIVER);
			//throw new Exception(JDBCCnnMgrConstants.NULL_DRIVER);
		}
		if (url==null||url.trim().equals("")) {
			Logger.log(Logger.WARN, JDBCCnnMgrConstants.NULL_URL);
			//throw new Exception(JDBCCnnMgrConstants.NULL_URL);
		}
		if (user==null||user.trim().equals("")) {
			Logger.log(Logger.WARN, JDBCCnnMgrConstants.NULL_USER);
		}
		if (password == null || password.trim().equals("")) {
			Logger.log(Logger.WARN, JDBCCnnMgrConstants.NULL_PASSWORD);
		}
		if(backDir==null||backDir.trim().equals("")){
			Logger.log(Logger.WARN,JDBCCnnMgrConstants.NULL_BACKDIR);
		}
		
		/*if(osPassword==null||osPassword.trim().equals("")){
			Logger.log(Logger.WARN,JDBCCnnMgrConstants.NULL_OSPASSWORD);
		}*/

    	this.poolName=poolName; 
    	this.driver=driver;
    	this.url=url;
    	this.dbUser=user;
    	this.dbPassword =password;
    	
    	this.backDir=backDir;
//    	this.osUser=osUser;
    	//this.osPassword=osPassword;
    	
    	this.poolMaxSize=poolMaxSize;
    	this.poolMinSize=poolMinSize;
    	this.increaseStep=increaseStep;
		this.scanCount=scanCount;
		this.scanInterval=scanInterval;
			    	
    	connPool=new ArrayList(poolMaxSize);
    	
    	//try{
    		//加载jdbc驱动程序
			//Class.forName(driver);
		try {
			for(int i=0;i<poolMinSize;i++){
				conn = DriverManager.getConnection(this.url,this.dbPassword,this.dbPassword);
			
				String poolID=driver.trim()+url.trim()+"."+user.trim()+"."+password.trim();
				connPool.add(new JDBCCnn(poolID,conn,this.url,this.dbUser,this.dbPassword,this.backDir));
				
			}
		} catch (Exception e) {
    		String errMsg=ExceptionHandle.getMessage(e);
    		Logger.log(Logger.ERROR,JDBCCnnMgrConstants.EXCEP_POOL_CREATING);	
    		Logger.log(Logger.ERROR,errMsg);	

    		throw new JDBCException(errMsg);
		} 
		
    	//启动空闲连接扫描任务
    	this.freeScan.schedule(new TimerTask(){
    			public void run(){
    				poolDecrease();
    			}
    		},
    		JDBCPool.DELAY,
    		scanInterval);
   }

   /**
    * 构造函数,根据参数给定的属性集合创建连接池
    * @Precondition attr不能为空
    * @Postcondition 根据参数给定的属性集合创建连接池
    * @param attr	属性集合对象
    * @exception 连接规范为空或者创建连接对象时出错
    */
   public JDBCPool(PoolSpec attr)
   throws JDBCException
   {
    	Connection conn;
    	
    	if(attr==null) throw new JDBCException(JDBCCnnMgrConstants.NULL_SPEC);
    	this.poolName=attr.poolName;
    	this.driver=attr.driver;
    	this.url=attr.url;
    	this.dbUser=attr.dbUser;
    	this.dbPassword =attr.dbPassword;
    	this.backDir=attr.backDir;
    	//this.osUser=attr.osUser;
    	//this.osPassword=attr.osPassword;
    	
    	this.poolMaxSize=attr.poolMaxSize;
    	this.poolMinSize=attr.poolMinSize;
    	this.increaseStep=attr.increaseStep;
		this.scanCount=attr.scanCount;
		this.scanInterval=attr.scanInterval;
		
		
		    	
    	connPool=new ArrayList(poolMinSize);
    	//try{
    		//加载jdbc驱动程序
			//Class.forName(driver);
			//System.out.println(driver);
		try {
			for(int i=0;i<poolMinSize;i++){
				conn = DriverManager.getConnection(url,dbUser,dbPassword);
			
				String poolID=driver.trim()+url.trim()+"."+dbUser.trim()+"."+dbPassword.trim();
				connPool.add(new JDBCCnn(poolID,conn,url,dbUser,dbPassword,backDir));
				//isUsing[i]=false;
			}
		} catch (SQLException e) {
    		String errMsg=ExceptionHandle.getMessage(e);
    		Logger.log(Logger.ERROR,JDBCCnnMgrConstants.EXCEP_POOL_CREATING);	
    		Logger.log(Logger.ERROR,errMsg);	
    		throw new JDBCException(errMsg);
		}
    	//启动空闲连接扫描任务
    	this.freeScan.schedule(new TimerTask(){
    			public void run(){
    				poolDecrease();
    			}
    		},
    		JDBCPool.DELAY,
    		this.scanInterval);
   }

	/**
	 * 返回连接池的大小
	 * @return 连接池的大小
	 * */   
   private int size()
   {
   		return connPool.size();
   }
   
   /**
    * 判断连接池是否还有空闲连接
    * @return	true:连接池没有空闲连接	false:连接池还有空闲连接
    * */
   private boolean isFull()
   {
   		JDBCCnn cnn;   
   		//int poolsize=size();
   		Iterator iterator=connPool.iterator();
   		while(iterator.hasNext()){
   			cnn=(JDBCCnn)iterator.next();
   			if(cnn.status==JDBCCnn.FREE){
   				return false;
   			}
   		}
   		return true;
   }
   
   /**
    * 判断连接池中的连接是否全部处于空闲状态
    * @return true:连接池中的连接是否全部空闲 false:连接池中有非空闲状态的连接
    * */
	private boolean isAllFree(){
   		JDBCCnn cnn;   
   		//int poolsize=size();
   		Iterator iterator=connPool.iterator();
		while(iterator.hasNext()){
			cnn=(JDBCCnn)iterator.next();
			if(cnn.status!=JDBCCnn.FREE){
				return false;
			}
		}	
		return true;
	}

	/**
	 * 判断连接池中的是否有连接出于执行状态
	 * @return true:连接池中的有连接出于执行状态 false:连接池中的没有连接出于执行状态
	 * */
	private boolean isNoExecuting(){
   		JDBCCnn cnn;   
   		//int poolsize=size();
   		Iterator iterator=connPool.iterator();
		while(iterator.hasNext()){
			cnn=(JDBCCnn)iterator.next();
			if(cnn.status==JDBCCnn.EXECUTING){
				return true;
			}		
		}	
		return false;
	}
	
   /**
    * 从connPool的头开始查找,找到第一个空闲连接对象并且返回该对象。
    * */
   private JDBCCnn getFree(){
   		JDBCCnn cnn=null;   
   		Iterator iterator=connPool.iterator();
   		while(iterator.hasNext()){
   			cnn=(JDBCCnn)iterator.next();
   			if(cnn.status==JDBCCnn.FREE){
   				cnn.status=JDBCCnn.TAKING;
   				return cnn;
   			}
   		}
   		//没有空闲连接
   		return null;
   }
   
   public boolean hasConnect(){
   		JDBCCnn cnn=null;
   		boolean result=true;
   		System.out.println("call hasConnect");
   		try {
			cnn=getFreeConnection(1000);
		} catch (JDBCException e) {
		}
		if(cnn==null) result=false;
		else {
			result=true;
			this.releaseConnection(cnn);
		}
		return result;   
   }
   
   /**
    * 关闭连接池,在此之前必须关闭连接池中所有的连接
    */
   protected void shutdown(int level) 
   throws JDBCException
   {
   		Iterator iterator=connPool.iterator();
   		JDBCCnn cnn;
   		String errMsg="";
   		if(isShutting){
   			throw new JDBCException(JDBCCnnMgrConstants.EXCEP_SHUTTING);
   		}else{
			isShutting=true;   		
   		}
   		
   		try{
   			//释放连接
   			if(level==JDBCPool.TRANSACTIONAL){
		   		while(iterator.hasNext()){
		   			cnn=(JDBCCnn)iterator.next();
		   			synchronized(cnn.transLocker){
		   				if(cnn.status==JDBCCnn.EXECUTING){
		   					//如果还有操作没有执行完毕,则等待
		   					cnn.transLocker.wait();
		   				}
		   				//System.out.println("Wait end:"+System.currentTimeMillis());
			   			cnn.close();
		   			}
		   		}
		    	//关闭列表			
		    	connPool.clear();
   			}else if(level==JDBCPool.ABORT){
		   		while(iterator.hasNext()){
		   			cnn=(JDBCCnn)iterator.next();
			   		cnn.close();
		   		}
		    	//关闭列表			
		    	connPool.clear();
   			}else{
   				//参数错误
   				throw new JDBCException(JDBCCnnMgrConstants.INVALID_PARAM_FOR_SHUTDOWN);
   			}
   			freeScan.cancel();
    	}catch(Exception e){
    		errMsg=ExceptionHandle.getMessage(e);
    		Logger.log(Logger.ERROR,JDBCCnnMgrConstants.EXCEP_POOL_SHUTDOWN);
    		Logger.log(Logger.ERROR,e.getMessage());
    		throw new JDBCException(errMsg);
    	}
   }
   
	public int executeUpdate(String sql)throws JDBCException{
		int result;
		JDBCCnn cnn=this.getFreeConnection();				
		try {
			result=cnn.executeUpdate(sql);
			this.releaseConnection(cnn);
		} catch (JDBCException e) {
			if(cnn!=null)	this.releaseConnection(cnn);
			e.printStackTrace();
			throw e;
		}
		
		return result;
	}
	
	public ResultSet executeQuery(String sql,int ResultSetType)throws JDBCException{		
		JDBCCnn cnn=this.getFreeConnection();
		ResultSet rs=null;
		try {
			rs =cnn.executeQuery(sql,ResultSetType);
			this.releaseConnection(cnn);
		} catch (JDBCException e) {
			if(cnn!=null)	this.releaseConnection(cnn);
			e.printStackTrace();
			throw e;
		}
						
		return rs;	
	}

	public ResultSet executeQuery(String sql)throws JDBCException{
		JDBCCnn cnn=this.getFreeConnection();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -