亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

一個 JDBC 連接池例子

2019-11-18 12:40:33
字體:
來源:轉載
供稿:網友

  import java.io.*;
  import java.sql.*;
  import java.util.*;
  import java.util.Date;
  
  /**
  * This class is a Singleton that PRovides access to one or many
  * connection pools defined in a Property file. A client gets
  * access to the single instance through the static getInstance()
  * method and can then check-out and check-in connections from a pool.
  * When the client shuts down it should call the release() method
  * to close all open connections and do other clean up.
  */
  public class DBConnectionManager {
   static private DBConnectionManager instance; // The single instance
   static private int clients;
  
   private Vector drivers = new Vector();
   private PrintWriter log;
   private Hashtable pools = new Hashtable();
  
   /**
   * Returns the single instance, creating one if it's the
   * first time this method is called.
   *
   * @return DBConnectionManager The single instance.
   */
   static synchronized public DBConnectionManager getInstance() {
   if (instance == null) {
   instance = new DBConnectionManager();
   }
   clients++;
   return instance;
   }
  
   /**
   * A private constrUCtor since this is a Singleton
   */
   private DBConnectionManager() {
   init();
   }
  
   /**
   * Returns a connection to the named pool.
   *
   * @param name The pool name as defined in the properties file
   * @param con The Connection
   */
   public void freeConnection(String name, Connection con) {
   DBConnectionPool pool = (DBConnectionPool) pools.get(name);
   if (pool != null) {
   pool.freeConnection(con);
   }
   }
  
   /**
   * Returns an open connection. If no one is available, and the max
   * number of connections has not been reached, a new connection is
   * created.
   *
   * @param name The pool name as defined in the properties file
   * @return Connection The connection or null
   */
   public java.sql.Connection getConnection(String name) {
   DBConnectionPool pool = (DBConnectionPool) pools.get(name);
   if (pool != null) {
   return pool.getConnection();
   }
   return null;
   }
  
   /**
   * Returns an open connection. If no one is available, and the max
   * number of connections has not been reached, a new connection is
   * created. If the max number has been reached, waits until one
   * is available or the specified time has elapsed.
   *
   * @param name The pool name as defined in the properties file
   * @param time The number of milliseconds to wait
   * @return Connection The connection or null
   */
   public java.sql.Connection getConnection(String name, long time) {
   DBConnectionPool pool = (DBConnectionPool) pools.get(name);
   if (pool != null) {
   return pool.getConnection(time);
   }
   return null;
   }
  
   /**
   * Closes all open connections and deregisters all drivers.
   */
   public synchronized void release() {
   // Wait until called by the last client
   if (--clients != 0) {
   return;
   }
  
   Enumeration allPools = pools.elements();
   while (allPools.hasMoreElements()) {
   DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
   pool.release();
   }
   Enumeration allDrivers = drivers.elements();
   while (allDrivers.hasMoreElements()) {
   Driver driver = (Driver) allDrivers.nextElement();
   try {
   DriverManager.deregisterDriver(driver);
   log("Deregistered JDBC driver " + driver.getClass().getName());
   }
   catch (SQLException e) {
   log(e, "Can't deregister JDBC driver: " + driver.getClass().getName());
   }
   }
   }
  
   /**
   * Creates instances of DBConnectionPool based on the properties.
   * A DBConnectionPool can be defined with the following properties:
   *

   * <poolname>.url The JDBC URL for the database
   * <poolname>.user A database user (optional)
   * <poolname>.passWord A database user password (if user specified)
   * <poolname>.maxconn The maximal number of connections (optional)
   *

   *
   * @param props The connection pool properties
   */
   private void createPools(Properties props) {
   Enumeration propNames = props.propertyNames();
   while (propNames.hasMoreElements()) {
   String name = (String) propNames.nextElement();
   if (name.endsWith(".url")) {
   String poolName = name.substring(0, name.lastIndexOf("."));
   String url = props.getProperty(poolName + ".url");
   if (url == null) {
   log("No URL specified for " + poolName);
   continue;
   }
   String user = props.getProperty(poolName + ".user");
   String password = props.getProperty(poolName + ".password");
   String maxconn = props.getProperty(poolName + ".maxconn", "0");
   int max;
   try {
   max = Integer.valueOf(maxconn).intvalue();
   }
   catch (NumberformatException e) {
   log("Invalid maxconn value " + maxconn + " for " + poolName);
   max = 0;
   }
   DBConnectionPool pool =
   new DBConnectionPool(poolName, url, user, password, max);
   pools.put(poolName, pool);
   log("Initialized pool " + poolName);
   }
   }
   }
  
   /**
   * Loads properties and initializes the instance with its values.
   */
   private void init() {
   InputStream is = getClass().getResourceAsStream("/db.properties");
   Properties dbProps = new Properties();
   try {
   dbProps.load(is);
   }
   catch (Exception e) {
   System.err.println("Can't read the properties file. " +
   "Make sure db.properties is in the CLASSPATH");
   return;
   }
   String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");
   try {
   log = new PrintWriter(new FileWriter(logFile, true), true);
   }
   catch (IOException e) {
   System.err.println("Can't open the log file: " + logFile);
   log = new PrintWriter(System.err);
   }
   loadDrivers(dbProps);
   createPools(dbProps);
   }
  
   /**
   * Loads and registers all JDBC drivers. This is done by the
   * DBConnectionManager, as opposed to the DBConnectionPool,
   * since many pools may share the same driver.
   *
   * @param props The connection pool properties

上一篇:JDBC 解決方案

下一篇:JDBC3.0 新特性

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲va久久久噜噜噜| 亚洲美女福利视频网站| 色悠悠国产精品| 91国产视频在线播放| 欧美性受xxx| 亚洲精品美女久久| 成人高h视频在线| 国产一区二区三区在线观看视频| 91精品久久久久久久久久久久久久| 国产精品中文字幕在线观看| 久久人人爽人人爽人人片亚洲| 黄色一区二区在线| 成人网在线免费观看| 日韩久久精品成人| 久久久精品一区二区| 久久影视免费观看| 久久久99免费视频| 日韩av在线资源| 2021久久精品国产99国产精品| 国产成+人+综合+亚洲欧洲| 国产精品视频网址| 欧美日韩国产一区在线| www.欧美免费| 亚洲精品有码在线| 久久久国产在线视频| 亚洲午夜久久久影院| 久久亚洲电影天堂| 亚洲国产欧美自拍| 日韩暖暖在线视频| 亚洲欧美综合图区| 亚洲日韩欧美视频| 都市激情亚洲色图| 亚洲欧美中文字幕在线一区| 亚洲成人久久网| 精品中文字幕在线2019| 国产91精品最新在线播放| 国产视频精品免费播放| 最好看的2019年中文视频| 成人www视频在线观看| 久久久欧美一区二区| 日韩欧美成人免费视频| 日韩在线观看视频免费| 最新国产精品拍自在线播放| 川上优av一区二区线观看| 国产精品午夜视频| 午夜精品一区二区三区在线| 九九热99久久久国产盗摄| 亚洲成人在线网| 日韩成人av网址| 亚洲综合日韩中文字幕v在线| 91九色视频在线| 欧美黑人极品猛少妇色xxxxx| 午夜精品久久久久久久99黑人| 欧洲午夜精品久久久| 日韩精品免费一线在线观看| 国产成人精品国内自产拍免费看| 伊人久久综合97精品| 欧美成人免费观看| 国产成人综合亚洲| 91久久久久久久| 中文字幕9999| 欧美精品一区二区免费| 日韩在线观看免费| 亚洲欧洲日产国产网站| 欧美老少做受xxxx高潮| 国产一区二区三区在线| 日韩欧美在线视频| 国产欧美最新羞羞视频在线观看| 欧美性xxxxxxxxx| 国产精品扒开腿爽爽爽视频| 亚洲人成在线观看网站高清| 高清欧美性猛交xxxx黑人猛交| 成人网中文字幕| 日韩精品在线私人| 国产成人亚洲综合91| 中文字幕精品久久久久| 亚洲第一天堂av| 丝袜亚洲另类欧美重口| 久精品免费视频| 久久99热这里只有精品国产| 久久99久久99精品中文字幕| 欧美激情视频网站| 国产精品久久久久久久av大片| 高跟丝袜一区二区三区| 日韩视频在线一区| 日韩精品视频免费在线观看| 日韩av综合网| 国产在线高清精品| 成人美女av在线直播| 国产精品嫩草影院一区二区| 日韩三级成人av网| 久久综合久中文字幕青草| 久久久久中文字幕2018| 国产丝袜精品第一页| 疯狂做受xxxx高潮欧美日本| 国产精品久久久久福利| 精品亚洲一区二区三区| 欧美成人午夜激情在线| 亚洲精品综合精品自拍| 黑人狂躁日本妞一区二区三区| 国产成+人+综合+亚洲欧美丁香花| 日韩大片在线观看视频| 亚洲国产成人精品电影| 欧美日韩激情网| 精品国产成人在线| 欧美日韩美女在线| 亚洲成人亚洲激情| 亚洲精品v欧美精品v日韩精品| 国产丝袜精品视频| 国产伊人精品在线| 亚洲无av在线中文字幕| 国产精品自产拍在线观看| 中文字幕日韩在线观看| 97香蕉超级碰碰久久免费软件| 国产精品白丝jk喷水视频一区| 亚州av一区二区| 性欧美视频videos6一9| 日韩电影中文字幕在线| 日韩中文在线视频| 色与欲影视天天看综合网| 日本aⅴ大伊香蕉精品视频| 一区二区三区国产在线观看| 欧美亚州一区二区三区| 国内精品模特av私拍在线观看| 国产中文字幕91| 欧美高清自拍一区| 综合av色偷偷网| 欧美日韩在线一区| 国产91成人video| 日本韩国在线不卡| 亚洲国产日韩欧美在线动漫| 欧美一级黑人aaaaaaa做受| 亚洲日本成人女熟在线观看| 欧美激情高清视频| 欧美国产日韩一区二区| 中文字幕欧美日韩在线| 国产日韩精品视频| 欧美大尺度在线观看| 欧美激情18p| 国产精品久久久久高潮| 欧美日韩在线第一页| 亚洲a成v人在线观看| 国产精品色午夜在线观看| 成人激情视频小说免费下载| 欧美大片va欧美在线播放| 欧美精品18videos性欧| 久久久99久久精品女同性| 亚洲天堂男人天堂女人天堂| 欧美国产日韩一区| 亚洲97在线观看| 国产成人涩涩涩视频在线观看| 911国产网站尤物在线观看| 日韩精品中文字幕在线播放| 国产在线视频2019最新视频| 欧美日韩亚洲一区二区三区| 亚洲一区二区三区视频播放| 一道本无吗dⅴd在线播放一区| 少妇激情综合网| 日韩成人激情在线| 国产精品久久电影观看| 国产精品电影久久久久电影网| 国产精品v片在线观看不卡| 最新国产精品亚洲| 国产精品久久激情|