dbconnectionmanager.java

来自「公司自己开发的工作流引擎」· Java 代码 · 共 160 行

JAVA
160
字号
package cn.com.iaspec.workflow.db;

import java.sql.*;

import org.apache.log4j.*;

/**
 *
 * <p>Title:数据库连接池管理</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: IASPEC Technologies</p>
 * @author xiesonglin
 * @version 1.0
 */
public class DBConnectionManager{
  protected boolean useJNDIConnection=true;
  private DBConnectionPool pool;
  public static long connectCount=0;
  public static long freeCount=0;
  protected String dataBaseType="ORACLE";
  private static Logger logger=Logger.getLogger(DBConnectionManager.class);

  public void Close(PreparedStatement preStm,boolean closeConnection){
    logger.debug("begin Close(PreparedStatement preStm)...");
    try{
      if(preStm!=null){
        if(closeConnection){
          Connection connection=null;
          connection=preStm.getConnection();
          close(connection);
        }
        preStm.close();
        preStm=null;
      }
    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }

  public String getDataBaseType(){
    return dataBaseType==null?"":dataBaseType.toUpperCase();

  }

  public void Close(CallableStatement callStm,boolean closeConnection){
    logger.debug("begin Close(CallableStatement callStm)...");
    try{
      if(callStm!=null){
        if(closeConnection){
          Connection connection=null;
          connection=callStm.getConnection();
          close(connection);
        }
        callStm.close();
        callStm=null;
      }
    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }

  public void close(ResultSet resultSet){
    logger.debug("begin Close(ResultSet resultSet)...");
    try{
      if(resultSet!=null){
        resultSet.close();
        resultSet=null;
      }
    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }

  public void close(Statement statement,boolean closeConnection){
    logger.debug("begin Close(Statement statement)...");
    try{
      if(statement!=null){
        if(closeConnection){
          Connection connection=null;
          connection=statement.getConnection();
          close(connection);
        }
        statement.close();
        statement=null;
      }
    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }

  public synchronized void addConnectCount(){
    this.connectCount++;
    logger.debug("connectCount is:"+connectCount);
  }

  public synchronized void close(Connection connection){
    try{
      logger.debug("begin Close(Connection connection)");
      if(connection!=null){
        if(!useJNDIConnection){
          freeConnection(connection);
          this.freeCount++;
        }
        else{
          if(!connection.isClosed()){
            connection.close();
            this.freeCount++;
          }
          connection=null;
        }
      }
      logger.debug("freeCount is:"+freeCount);
    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }

  /**
   * rollback DB transaction
   * @param connection
   * @throws SQLException
   */
  public void rollBackConnection(Connection connection){
    if(connection!=null){
      try{
        if(!connection.getAutoCommit()){
          connection.rollback();
        }
      }
      catch(Exception e){
        e.printStackTrace();
      }
    }

  }

  /**
   * Free a connection
   * @param con connection
   * @throws SQLException this method
   */
  public void freeConnection(Connection con)
      throws SQLException{
    if(pool!=null){
      pool.freeConnection(con);
    }
  }

  public void setDataBaseType(String dataBaseType){
    this.dataBaseType=dataBaseType;
  }

}

⌨️ 快捷键说明

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