<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 189,comments - 115,trackbacks - 0
    一個很不錯的數(shù)據(jù)庫連接池實(shí)現(xiàn),備份之
    ?

    一個很不錯的數(shù)據(jù)庫連接池實(shí)現(xiàn),備份之

    import java.sql.*;
    import java.io.*;
    import java.util.*;
    public class DBConnectionManager {

    ??private static int clients = 0;
    ??private static DBConnectionManager instance;
    ??private Vector drivers = new Vector();
    ??private PrintWriter log;
    ??private Hashtable pools = new Hashtable();


    ??public DBConnectionManager() {
    ????init();
    ??}
    ??private void log(String msg) {
    ????log.println(new java.util.Date() + ": " + msg);
    ??}

    ??/**
    ??* 將文本信息與異常寫入日志文件
    ??*/
    ??private void log(Throwable e, String msg) {
    ????log.println(new java.util.Date() + ": " + msg);
    ????e.printStackTrace(log);
    ??}

    ??public static synchronized??DBConnectionManager getInstance() {
    ????if (instance == null) {
    ??????instance = new DBConnectionManager();
    ????}
    ????clients++;
    ????return instance;
    ??}
    ??private void init() {
    ????InputStream is = getClass().getResourceAsStream("/db.properties");
    ????Properties dbProps = new Properties();
    ????try {
    ??????dbProps.load(is);
    ????}
    ????catch (Exception e) {
    ????????System.err.println("Can not read the properties file. " +
    ????????"Make sure db.properties is in the CLASSPATH");
    ????????return;
    ????}
    ????loadDrivers(dbProps);
    ????createPools(dbProps);
    ??}
    ??public void freeConnection(String name, Connection con) {
    ????DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    ????if (pool != null) {
    ??????pool.freeConnection(con);
    ????}
    ??}
    ??public Connection getConnection(String name) {
    ????DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    ????if (pool != null) {
    ??????return pool.getConnection();
    ????}
    ????return null;
    ??}
    ??public Connection getConnection(String name, long time) {
    ????DBConnectionPool pool = (DBConnectionPool) pools.get(name);
    ????if (pool != null) {
    ??????return pool.getConnection(time);
    ????}
    ????return null;
    ??}
    ??public synchronized void release() {
    ????// 等待直到最后一個客戶程序調(diào)用
    ????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("撤銷JDBC驅(qū)動程序 " + driver.getClass().getName()+"的注冊");
    ??????}
    ??????catch (SQLException e) {
    ??????log(e, "無法撤銷下列JDBC驅(qū)動程序的注冊: " + driver.getClass().getName());
    ??????}
    ????}
    ??}


    /*
    drivers=sun.jdbc.odbc.JdbcOdbcDriver jdbc.idbDriver

    logfile=D:\\user\\src\\java\\DBConnectionManager\\log.txt

    idb.url=jdbc:idb:c:\\local\\javawebserver1.1\\db\\db.prp

    idb.maxconn=2

    access.url=jdbc:odbc:demo

    access.user=demo

    access.password=demopw

    */
    ??private void loadDrivers(Properties props) {
    ????String driverClasses = props.getProperty("drivers");
    ????StringTokenizer st = new StringTokenizer(driverClasses);
    ????while (st.hasMoreElements()) {
    ??????String driverClassName = st.nextToken().trim();
    ??????try {
    ????????Driver driver = (Driver)
    ??????????Class.forName(driverClassName).newInstance();
    ????????DriverManager.registerDriver(driver);
    ????????drivers.addElement(driver);
    ?????//???Log.log("Registered JDBC driver " + driverClassName);
    ??????}
    ??????catch (Exception e) {
    ??????//??Log.log("Can not register JDBC driver: " + driverClassName + ", Exception: " + e.toString()));
    ??????}
    ????}
    ??}
    ??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.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.log("Invalid maxconn value " + maxconn + " for " +???poolName);
    ??????????max = 0;
    ????????}
    ????????DBConnectionPool pool =
    ??????????new DBConnectionPool(poolName, url, user, password, max);
    ????????pools.put(poolName, pool);
    ??????//??Log.log("Initialized pool " + poolName);
    ??????}
    ????}
    ??}

    ??class DBConnectionPool {
    ????private int checkedOut;
    ????private Vector freeConnections = new Vector();
    ????private int maxConn;
    ????private String name;
    ????private String password;
    ????private String URL;
    ????private String user;

    ????/**
    ????* 創(chuàng)建新的連接池
    ????*
    ????* @param name 連接池名字
    ????* @param URL 數(shù)據(jù)庫的JDBC URL
    ????* @param user 數(shù)據(jù)庫帳號,或 null
    ????* @param password 密碼,或 null
    ????* @param maxConn 此連接池允許建立的最大連接數(shù)
    ????*/
    ????public DBConnectionPool(String name, String URL, String user
    ????????, String password,???int maxConn)
    ????{
    ??????this.name = name;
    ??????this.URL = URL;
    ??????this.user = user;
    ??????this.password = password;
    ??????this.maxConn = maxConn;
    ????}

    ??/**
    ??* 將不再使用的連接返回給連接池
    ??*
    ??* @param con 客戶程序釋放的連接
    ??*/
    ????public synchronized void freeConnection(Connection con) {
    ????// 將指定連接加入到向量末尾
    ??????freeConnections.addElement(con);
    ??????checkedOut--;
    ??????notifyAll();
    ????}

    ????/**
    ?????* 從連接池獲得一個可用連接.如沒有空閑的連接且當(dāng)前連接數(shù)小于最大連接
    ?????* 數(shù)限制,則創(chuàng)建新連接.如原來登記為可用的連接不再有效,則從向量刪除之,
    ?????* 然后遞歸調(diào)用自己以嘗試新的可用連接.
    ?????*/
    ????public synchronized Connection getConnection()
    ????{
    ??????Connection con = null;
    ??????if (freeConnections.size() > 0)
    ??????{
    ??????// 獲取向量中第一個可用連接
    ????????con = (Connection) freeConnections.firstElement();
    ????????freeConnections.removeElementAt(0);
    ????????try {
    ??????????if (con.isClosed())
    ??????????{
    ????????????log("從連接池" + name+"刪除一個無效連接");
    ????????????// 遞歸調(diào)用自己,嘗試再次獲取可用連接
    ????????????con = getConnection();
    ??????????}
    ????????}
    ????????catch (SQLException e)
    ????????{
    ??????????log("從連接池" + name+"刪除一個無效連接");
    ??????????// 遞歸調(diào)用自己,嘗試再次獲取可用連接
    ??????????con = getConnection();
    ????????}
    ??????}
    ??????else if (maxConn == 0 || checkedOut < maxConn)
    ??????{
    ????????con = newConnection();
    ??????}
    ??????if (con != null) {
    ????????checkedOut++;
    ??????}
    ??????return con;
    ????}

    ??/**
    ??* 從連接池獲取可用連接.可以指定客戶程序能夠等待的最長時間
    ??* 參見前一個getConnection()方法.
    ??*
    ??* @param timeout 以毫秒計的等待時間限制
    ??*/
    ????public synchronized Connection getConnection(long timeout)
    ????{
    ??????long startTime = new java.util.Date().getTime();
    ??????Connection con;
    ??????while ((con = getConnection()) == null)
    ??????{
    ????????try {
    ??????????wait(timeout);
    ????????}
    ????????catch (InterruptedException e) {}
    ????????if ((new java.util.Date().getTime() - startTime) >= timeout)
    ????????{
    ????????// wait()返回的原因是超時
    ??????????return null;
    ????????}
    ??????}
    ??????return con;
    ????}

    ??/**
    ??* 關(guān)閉所有連接
    ??*/
    ????public synchronized void release()
    ????{
    ??????Enumeration allConnections = freeConnections.elements();
    ??????while (allConnections.hasMoreElements())
    ??????{
    ????????Connection con = (Connection) allConnections.nextElement();
    ????????try {
    ????????con.close();
    ??????????log("關(guān)閉連接池" + name+"中的一個連接");
    ????????}
    ????????catch (SQLException e) {
    ??????????log(e, "無法關(guān)閉連接池" + name+"中的連接");
    ????????}
    ??????}
    ??????freeConnections.removeAllElements();
    ????}

    ??/**
    ??* 創(chuàng)建新的連接
    ??*/
    ????private Connection newConnection()
    ????{
    ??????Connection con = null;
    ??????try {
    ????????if (user == null) {
    ??????????con = DriverManager.getConnection(URL);
    ????????}
    ????????else {
    ??????????con = DriverManager.getConnection(URL, user, password);
    ????????}
    ????????log("連接池" + name+"創(chuàng)建一個新的連接");
    ??????}
    ??????catch (SQLException e) {
    ????????log(e, "無法創(chuàng)建下列URL的連接: " + URL);
    ????????return null;
    ??????}
    ??????return con;
    ????}
    ??}
    }

    posted on 2006-03-25 20:28 MEYE 閱讀(345) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 69式国产真人免费视频| 免费成人黄色大片| 亚洲国产欧美国产综合一区 | 精品国产香蕉伊思人在线在线亚洲一区二区| 瑟瑟网站免费网站入口| 亚洲av中文无码乱人伦在线播放| 免费在线观看h片| 极品美女一级毛片免费| 久久亚洲一区二区| 日本a级片免费看| 你懂得的在线观看免费视频| 亚洲高清资源在线观看| 免费国产怡红院在线观看| 久久国产精品免费专区| 亚洲国产成人AV在线播放| 久久被窝电影亚洲爽爽爽| 成人黄动漫画免费网站视频| 久久精品成人免费观看97| 亚洲最大的成人网| 久久亚洲精品成人777大小说| 午夜免费福利在线观看| 免费A级毛片无码专区| 男人的天堂av亚洲一区2区| 亚洲国产另类久久久精品黑人| 成人奭片免费观看| 久久国产乱子伦精品免费不卡| 精品韩国亚洲av无码不卡区| 亚洲自偷自拍另类图片二区| 亚洲男人天堂2020| 成人免费无毒在线观看网站| 无码成A毛片免费| 黄床大片免费30分钟国产精品| 天堂亚洲国产中文在线| 亚洲嫩模在线观看| 亚洲乱码日产精品a级毛片久久| 在线视频免费观看高清| 少妇无码一区二区三区免费| 美女无遮挡拍拍拍免费视频| 亚洲AV一区二区三区四区| 亚洲人成高清在线播放| 亚洲自偷自拍另类12p|