想做一个WEB应用探测一下我的数据库状态是否正常的。
如果只是做一个简单的DB连接看是否有异常出现。
这样感觉不会探测出来DB是否有堵塞?
如何探测是否有DB堵塞呢

解决方案 »

  1.   


    package kjb.main;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import java.util.Date;
    public class DBConnectionManager
    {
    static private DBConnectionManager instance; // 唯一实例
    static private int clients; private Vector drivers = new Vector();
    private PrintWriter log;
    public static Hashtable pools = new Hashtable();  
    static synchronized public DBConnectionManager getInstance()
    {
    instance = new DBConnectionManager();
    clients++;
    return instance;
    } public static int getCurConns(String name)
    {
    DBConnectionPool pool = (DBConnectionPool)pools.get(name);
    if(pool!=null)
    {
    return pool.getCurConns();
    }
    else
    {
    return 0;
    }
    } public static int getSumConns(String name)
    {
    DBConnectionPool pool = (DBConnectionPool)pools.get(name);
    if(pool!=null)
    {
    return pool.getSumConns();
    }
    else
    {
    return 0;
    }
    } public static int getMaxConns(String name)
    {
    DBConnectionPool pool = (DBConnectionPool)pools.get(name);
    if(pool!=null)
    {
    return pool.getMaxConns();
    }
    else
    {
    return 0;
    }
    } public static int getNullPool(String name)
    {
    DBConnectionPool pool = (DBConnectionPool)pools.get(name);
    if(pool!=null)
    {
    return pool.getNullPool();
    }
    else
    {
    return 0;
    }
    } public static int getOracleErr(String name)
    {
    DBConnectionPool pool = (DBConnectionPool)pools.get(name);
    if(pool!=null)
    {
    return pool.getOracleErr();
    }
    else
    {
    return 0;
    }
    } private DBConnectionManager()
    {
    init();
    } 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()
    {
    // 等待直到最后一个客户程序调用
    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驱动程序 " + driver.getClass().getName() + "的注册");
    }
    catch(SQLException e)
    {
    log(e, "无法撤销下列JDBC驱动程序的注册: " + driver.getClass().getName());
    }
    }
    } /**
     * 根据指定属性创建连接池实例.
     */
    private void createPools()
    {
    String poolName = Configuration.ConnectionPoolName;
    String url = Configuration.DB_URL;
    if(url == null)
    {
    log("没有为连接池" + poolName + "指定URL");
    return ;
    }
    String user = Configuration.DB_USERNAME;
    String password = Configuration.DB_PASSWORD;
    String maxconn = Configuration.DB_MAXCONNNUM;
    int max;
    try
    {
    max = Integer.valueOf(maxconn).intValue();
    }
    catch(NumberFormatException e)
    {
    log("错误的最大连接数限制: " + maxconn + " .连接池: " + poolName);
    max = 0;
    }
    DBConnectionPool pool =
    new DBConnectionPool(poolName, url, user, password, max);
    pools.put(poolName, pool);
    log("成功创建连接池" + poolName); }
    private void init()
    {
     String logFile = Configuration.DB_LOGFILE;
     try {
       log = new PrintWriter(new FileWriter(logFile, true), true);
     }
     catch (IOException e) {
       System.err.println("无法打开日志文件: " + logFile);
       log = new PrintWriter(System.err);
     } loadDrivers();
    createPools();
    }
    private void loadDrivers()
    {
    String driverClasses = Configuration.DB_JDBCDRIVER;
    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("成功注册JDBC驱动程序" + driverClassName);
    }
    catch(Exception e)
    {
    log("无法注册JDBC驱动程序: " +
    driverClassName + ", 错误: " + e);
    System.out.println("\r\n无法注册JDBC驱动程序: " +
    driverClassName + ", 错误: " + e);
    }
    }
    }
    private void log(String msg)
    {
    log.println(new Date() + ": " + msg);
    }
    private void log(Throwable e, String msg)
    {
    log.println(new Date() + ": " + msg);
    e.printStackTrace(log);
    } class DBConnectionPool
    {
    private int sum;
    private int checkedOut;
    private Vector freeConnections = new Vector();
    private int maxConn;
    private int max;
    private String name;
    private String password;
    private String URL;
    private String user;
    private int isNullPool;
    private int isOracleErr ;
    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;
    } public synchronized void freeConnection(Connection con)
    {
    if(con!=null)
    {
    // 将指定连接加入到向量末尾
    freeConnections.addElement(con);
    checkedOut--;
    }
    notifyAll();
    } public synchronized Connection getConnection()
    {
    Connection con = null;
    if(freeConnections.size() > 0)
    {
    // 获取向量中第一个可用连接
    con = (Connection)freeConnections.firstElement();
    freeConnections.removeElementAt(0);
    try
    {
    if(con.isClosed())
    {
    log("从连接池" + name + "删除一个无效连接");
    // 递归调用自己,尝试再次获取可用连接
    con = getConnection();
    }
    }
    catch(SQLException e)
    {
    log("从连接池" + name + "删除一个无效连接");
    // 递归调用自己,尝试再次获取可用连接
    con = getConnection();
    }
    }
    else if(maxConn == 0 || checkedOut < maxConn)
    {
    con = newConnection();
    }
    else
    {
    //System.out.println("  连接池取空,等待回收!");
    isNullPool++ ;
    }
    if(con != null)
    {
    checkedOut++;
    sum++;
    }
    return con;
    } /**
     * 从连接池获取可用连接.可以指定客户程序能够等待的最长时间
     * 参见前一个getConnection()方法.
     *
     * @param timeout 以毫秒计的等待时间限制
     */
    public synchronized Connection getConnection(long timeout)
    {
    long startTime = new Date().getTime();
    Connection con;
    while( (con = getConnection()) == null)
    {
    try
    {
    wait(timeout);
    }
    catch(InterruptedException e)
    {}
    if( (new Date().getTime() - startTime) >= timeout)
    {
    // wait()返回的原因是超时
    return null;
    }
    }
    return con;
    } /**
     * 关闭所有连接
     */
    public synchronized void release()
    {
    Enumeration allConnections = freeConnections.elements();
    while(allConnections.hasMoreElements())
    {
    Connection con = (Connection)allConnections.nextElement();
    try
    {
    con.close();
    log("关闭连接池" + name + "中的一个连接");
    }
    catch(SQLException e)
    {
    log(e, "无法关闭连接池" + name + "中的连接");
    }
    }
    freeConnections.removeAllElements();
    }