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

📄 drivermanagerconnectionprovider.java

📁 DriverManagerConnectionProvider.java is the common function connected to data base
💻 JAVA
字号:
/*
 * DriverManagerConnectionProvider.java Created on 2004-12-6 
 */
package com.darkblue.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DriverManagerConnectionProvider {

    private String _url;
    private String _driverClass;
    private String _userName;
    private String _password;

    private final ArrayList pool = new ArrayList();
    private int poolSize;
    private int checkedOut = 0;

    private static Log log = LogFactory
        .getLog(DriverManagerConnectionProvider.class);

    public DriverManagerConnectionProvider() {}

    public void configure(String driverClass, String url, String userName,
        String password) throws Exception {

        poolSize = 20;

        if (driverClass == null) {
            log.warn("no JDBC Driver class was specified");
        } else {
            try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException cnfe) {
                String msg = "JDBC Driver class not found: " + driverClass;
                log.fatal(msg);
                throw new Exception(msg);
            }
        }

        if (url == null) {
            String msg = "JDBC URL was not specified";
            log.fatal(msg);
            throw new Exception(msg);
        }

        _url = url;
        _userName = userName;
        _password = password;

        log.info("using driver: " + driverClass + " at URL: " + url);
        log.info("connection properties: ");

    }

    public Connection getConnection() throws SQLException {
        if (log.isTraceEnabled())
            log.trace("total checked-out connections: " + checkedOut);

        synchronized (pool) {
            if (!pool.isEmpty()) {
                int last = pool.size() - 1;
                if (log.isTraceEnabled()) {
                    log.trace("using pooled JDBC connection, pool size: "
                        + last);
                    checkedOut++;
                }
                Connection pooled = (Connection) pool.remove(last);
                return pooled;
            }
        }
        log.debug("opening new JDBC connection");
        Connection conn = DriverManager.getConnection(_url, _userName,
            _password);
        if (log.isDebugEnabled()) {
            log.debug("created connection to: " + _url + ", Isolation Level: "
                + conn.getTransactionIsolation());
        }
        if (log.isTraceEnabled())
            checkedOut++;
        return conn;

    }

    public void closeConnection(Connection conn) throws SQLException {

        if (log.isDebugEnabled())
            checkedOut--;

        synchronized (pool) {
            int currentSize = pool.size();
            if (currentSize < poolSize) {
                if (log.isTraceEnabled())
                    log.trace("returning connection to pool, pool size: "
                        + (currentSize + 1));
                pool.add(conn);
                return;
            }
        }

        log.debug("closing JDBC connection");

        try {
            conn.close();
        } catch (SQLException sqle) {
            logExceptions(sqle);
            throw sqle;
        }
    }

    protected void finalize() {
        close();
    }

    public void close() {

        log.info("cleaning up connection pool: " + _url);

        Iterator iter = pool.iterator();
        while (iter.hasNext()) {
            try {
                ((Connection) iter.next()).close();
            } catch (SQLException sqle) {
                log.warn("problem closing pooled connection", sqle);
            }
        }
        pool.clear();
    }

    public static void logExceptions(SQLException ex) {
        if (log.isErrorEnabled()) {
            if (log.isDebugEnabled())
                log.debug("SQL Exception", ex);
            while (ex != null) {
                log.warn(new StringBuffer(30).append("SQL Error: ").append(
                    ex.getErrorCode()).append(", SQLState: ").append(
                    ex.getSQLState()).toString());
                log.error(ex.getMessage());
                ex = ex.getNextException();
            }
        }
    }

    public static void main(String[] args) {

    }
}

⌨️ 快捷键说明

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