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

📄 util.java

📁 java 小型系统连接池参照适用于中小型网站
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package ntis.com.util;

import java.io.*;
import java.util.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.sql.*;
import ntis.com.base.RmiServer;
import ntis.com.util.*;

/**<p>
*奺庬嫟捠婡擻傪採嫙偡傞儐乕僥傿儕僥傿僋儔僗丅埲壓偺婡擻傪幚憰偟丄懠偺僋儔僗偵採嫙偡傞丅<br>
*丒弶婜壔僨乕僞僼傽僀儖偐傜偺僉乕抣専嶕<br>
*丒僨乕僞儀乕僗僐僱僋僔儑儞偺妋曐/夝曻<br>
*丒儘僌婰榐僣乕儖傊偺儊僢僙乕僕憲怣<br>
*</p><p>
*Singleton僷僞乕儞偺揔梡偵傛傝丄Util偺僀儞僗僞儞僗偼傾僾儕働乕僔儑儞拞偵堦偮偺傒偱偁傝丄
*偦偺偙偲偵傛偭偰儕僜乕僗偺堦尦娗棟偑払惉偝傟傞丅偦偺懚嵼偑堦偮偺傒偱偁傞偙偲傪曐徹偡傞堊偵丄
*Util偺僐儞僗僩儔僋僞偼private愰尵偝傟偰偍傝丄棙梡幰懁偱僀儞僗僞儞僗傪惗惉偡傞偙偲偼偱偒側偄丅
*Util偱掕媊偝傟偨婡擻傪巊梡偡傞応崌偵偼丄埲壓偺曽朄偱僀儞僗僞儞僗傪庢摼偡傞昁梫偑偁傞丅
*</p><p>
*<b>僀儞僗僞儞僗庢摼偺椺</b>:
*<pre>
*  Util util = Util.create();
*  util.儊僜僢僪柤();
*</pre>
*</p><p>
*create()偼Util偺僀儞僗僞儞僗偑傑偩懚嵼偟側偗傟偽怴偨偵惗惉偟偨僀儞僗僞儞僗傪屇傃弌偟尦偵曉偡丅
*婛偵僀儞僗僞儞僗偑懚嵼偡傞応崌偵偼惗惉偼峴傢偢丄尒偮偐偭偨僀儞僗僞儞僗傪曉偡丅
*</p>*/
public class Util {
  /**<p>
  *弶婜壔僼傽僀儖偺柤徧丅<BR>
  *僨傿僼僅儖僩偺弶婜壔僼傽僀儖偺柤徧傪曐帩偡傞僼傿乕儖僪丅
  *getInitParam()儊僜僢僪偺屇傃弌偟帪偵僼傽僀儖柤偲偟偰偙偺抣傪巜掕偡傞偲丄
  *僨傿僼僅儖僩弶婜壔僼傽僀儖偐傜僉乕偑専嶕偝傟傞丅
  *徻嵶偼getInitParam()儊僜僢僪偺愢柧傪嶲徠偺偙偲丅
  *</p>*/
  public static String DEFAULT_INIT_DATA = "DefaultInitData.txt";
  public static String INIT_DATA         = "InitData.txt";

  private static Hashtable initDataFiles = new Hashtable();

  private static Util util = null;
//private LogRecorder logRecorder = null;
  private RmiServer logRecorder = null;
  private int messageLevel = 0;
  private ConnectionData[] connectionDatas = {
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData(),
    new ConnectionData()
  };
  private int connectionCount = 0;

  private Util() {}

  private void init() {
    try {
      messageLevel = Integer.parseInt(Util.getInitParam(Util.DEFAULT_INIT_DATA, "MessageLevel"));
      String _serverName = Util.getInitParam(Util.DEFAULT_INIT_DATA, "LogRecorderName");
//    logRecorder = (LogRecorder)Naming.lookup(_serverName);
      logRecorder = (RmiServer)Naming.lookup(_serverName);
      Util.create().writeMessage0("Util : init()", "儘僌儗僐乕僟乕lookup姰椆");
    }
    catch(Exception e) {
      Util.create().writeExceptionMessage(
        "Util : init()", e,
        "儘僌儗僐乕僟乕傊偺lookup偑幐攕偟傑偟偨丅"
      );
    }

    try {
      String _driverName = Util.getInitParam(Util.DEFAULT_INIT_DATA, "DbDriverName");
      if(_driverName == "") {_driverName = "oracle.jdbc.driver.OracleDriver";}
      DriverManager.registerDriver ((Driver)Class.forName(_driverName).newInstance());
      Util.create().writeMessage0("Util : init()", "DB僪儔僀僶搊榐姰椆");
    }
    catch(Exception e) {
      Util.create().writeExceptionMessage(
        "Util : init()", e,
        "DB僪儔僀僶偺搊榐偑幐攕偟傑偟偨丅"
      );
    }
  }

  /**<p>
  *Util偺僀儞僗僞儞僗傪曉偡丅<BR>
  *Singleton僷僞乕儞偺揔梡偵傛傝丄偙偺僋儔僗偺僀儞僗僞儞僗偼傾僾儕働乕僔儑儞撪偱堦偮偺傒偲側傞丅
  *僐儞僗僩儔僋僞傪巊梡偟偨捠忢偺曽朄偵傛傞僀儞僗僞儞僗壔偼惂尷偝傟傞堊丄
  *Util偺婡擻傪巊梡偡傞応崌偼偙偺儊僜僢僪偺屇傃弌偟偵傛傝丄Util偺僀儞僗僞儞僗傪庢摼偡傞昁梫偑偁傞丅
  *</p>
  *@return Util偺僀儞僗僞儞僗丅
  */
  public static Util create() {
    if(util == null) {
      util = new Util();
      util.init();
    }
    return util;
  }

  //----------------------------------------------------------------------------

  /**<p>
  *僨乕僞儀乕僗僐僱僋僔儑儞傪妋曐偟丄屇傃弌偟尦傊曉偡丅<BR>
  *Util撪偱偼僨乕僞儀乕僗僐僱僋僔儑儞傪嵟戝16屄傑偱曐帩壜擻偱偁傝丄偦傟傜偺巊梡忬懺傪娗棟偟偰偄傞丅
  *偙偺儊僜僢僪傪捠偠偰僨乕僞儀乕僗僐僱僋僔儑儞偺庢摼偑梫媮偝傟傞偲丄
  *僾乕儖偟偰偄傞僐僱僋僔儑儞偵偮偄偰巊梡忬懺傪妋擣偟丄
  *嬻偄偰偄傞傕偺偑尒偮偐偭偨応崌偵偼偦偺僐僱僋僔儑儞傪曉偟丄
  *嬻偒偑柍偄応崌偵偼怴偨側僐僱僋僔儑儞傪惗惉偟偰梫媮尦偵曉偡丅
  *</p><p>
  *傑偨丄婛偵16屄偺僐僱僋僔儑儞傪曐帩偟偰偍傝丄偦傟傜偺拞偵嬻偒忬懺偺傕偺偑柍偄応崌偵偼丄
  *巊梡拞偺僐僱僋僔儑儞偺偳傟偐偑夝曻偝傟傞傑偱丄梫媮尦傪懸偨偣傞丅
  *</p>
  *@return 僨乕僞儀乕僗傊偺僐僱僋僔儑儞丅
  *@throws UnsatisfiedLinkError 儕儞僋僄儔乕丅JDBC僪儔僀僶偑尒偮偐傜側偄応崌丅
  *@throws SQLException         僨乕僞儀乕僗僐僱僋僔儑儞偺妋曐偵幐攕偟偨応崌丅
  *@throws Exception            僨乕僞儀乕僗僐僱僋僔儑儞偺妋曐偵幐攕偟偨応崌丅
  */
  public synchronized Connection openConnection() throws Exception {
    Util.create().writeMessage0("Util : openConnection()", "start.");

    Connection _connection = null;

    try {
      while(true) {
        for(int i = 0; i < connectionDatas.length; i++) {
          if(!connectionDatas[i].isReserved()) {
            _connection = connectionDatas[i].getConnection();

            if(_connection == null) {
              String _url      = Util.getInitParam(Util.DEFAULT_INIT_DATA, "DbUrl");
              String _user     = Util.getInitParam(Util.DEFAULT_INIT_DATA, "DbUser");
              String _passWord = Util.getInitParam(Util.DEFAULT_INIT_DATA, "DbPass");

              _connection = DriverManager.getConnection(_url, _user, _passWord);
              connectionDatas[i].setConnection(_connection);
            }

            connectionDatas[i].reserve();
            break;
          }
        }

        if(_connection == null) {
          Util.create().writeMessage1("Util : openConnection()", "wait.");
          wait();
          Util.create().writeMessage1("Util : openConnection()", "received notify.");
        }
        else {break;}
      } // while(true)
    }
    catch(UnsatisfiedLinkError e) {
      Exception ex = new Exception();
      Util.create().writeExceptionMessage(
        "Util : openConnection()", ex,
        "儕儞僋僄儔乕偑敪惗偟傑偟偨丅嫲傜偔JDBC僪儔僀僶傊偺僋儔僗僷僗偑愝掕偝傟偰偄傑偣傫丅"
      );
      throw ex;
    }
    catch(SQLException e) {
      Util.create().writeExceptionMessage(
        "Util : openConnection()", e,
        "Connection偺妋曐拞偵椺奜偑敪惗偟傑偟偨丅"
      );
      Util.create().writeSQLExceptionMessage(e);
      throw e;
    }
    catch(Exception e) {
      Util.create().writeExceptionMessage(
        "Util : openConnection()", e,
        "Connection偺妋曐拞偵椺奜偑敪惗偟傑偟偨丅"
      );
      throw e;
    }

    connectionCount++;

    Util.create().writeMessage1("Util : openConnection()", "愙懕悢 = " + String.valueOf(connectionCount));
    Util.create().writeMessage0("Util : openConnection()", "end.");
    return _connection;
  }

  /**<p>
  *搉偝傟偨僨乕僞儀乕僗僐僱僋僔儑儞傪枹巊梡偲儅乕僉儞僌偟丄懠偐傜偺嵞巊梡壜擻側忬懺偵偡傞丅<BR>
  *僐僱僋僔儑儞偺曉媝傪庴偗擖傟傞嵺偵丄
  *偦偺僐僱僋僔儑儞傪捠偠偰峴傢傟偨僩儔儞僓僋僔儑儞張棟偺廔椆忬懺傪妋擣偟丄
  *僩儔儞僓僋僔儑儞偑姰椆偟偰偄側偄応崌偵偼儘乕儖僶僢僋傪峴偆丅
  *儘乕儖僶僢僋偑峴傢傟偨帪偼丄摉奩僐僱僋僔儑儞偼僾乕儕儞僌偣偢偵攋婞偡傞丅
  *</p>
  *@param  connection 夝曻偡傞僨乕僞儀乕僗僐僱僋僔儑儞丅
  *@throws Exception  僨乕僞儀乕僗僐僱僋僔儑儞偺夝曻偵幐攕偟偨応崌丅
  */
  public synchronized void closeConnection(Connection connection) throws Exception {
    Util.create().writeMessage0("Util : closeConnection()", "start.");

    connectionCount--;

    if(connectionCount < 0) {connectionCount = 0;}

    try {
      for(int i = 0; i < connectionDatas.length; i++) {
        Util.create().writeMessage0(
          "Util : closeConnection()",
          "夝曻偝傟傞僐僱僋僔儑儞傪扵偟偰偄傑偡丅"
        );

        if(connectionDatas[i].getConnection() == connection) {
          connectionDatas[i].release();

          try {
            Util.create().writeMessage0(
              "Util : closeConnection()",
              "僩儔儞僓僋僔儑儞偺廔椆忬懺傪妋擣拞丅"
            );

            connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            connection.setAutoCommit(true);
          }
          catch(SQLException e) {
            Util.create().writeExceptionMessage(
              "Util : closeConnection()", e,
              "僩儔儞僓僋僔儑儞偑姰椆偟偰偄傑偣傫丅"
            );
            Util.create().writeSQLExceptionMessage(e);
            connectionDatas[i].setConnection(null);          // 00.12.14 捛壛
            Util.create().writeMessage1(                     // 00.12.14 捛壛
              "Util : closeConnection()",                    // 00.12.14 捛壛
              "僐僱僋僔儑儞傪攑婞偟傑偟偨丅"                 // 00.12.14 捛壛
            );                                               // 00.12.14 捛壛
            connection.rollback();
            connection.close();
            connection = null;
            Util.create().writeMessage1(
              "Util : closeConnection()",
              "儘乕儖僶僢僋傪幚巤偟傑偟偨丅"                 // 00.12.14 捛壛
            );
          }

          break;

⌨️ 快捷键说明

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