poolmanconnection.java

来自「Java Database connection pool」· Java 代码 · 共 491 行

JAVA
491
字号
/* *  PoolMan Java Object Pooling and Caching Library *  Copyright (C) 1999-2001 The Code Studio * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Lesser General Public License for more details. * *  The full license is located at the root of this distribution *  in the LICENSE file. */package com.codestudio.sql;// Code Studio Libraryimport com.codestudio.util.JDBCPool;import com.codestudio.util.ObjectPool;import com.codestudio.util.PooledObject;import javax.sql.ConnectionEvent;import javax.sql.ConnectionEventListener;import javax.sql.PooledConnection;import java.sql.*;import java.util.ArrayList;import java.util.Iterator;import java.util.WeakHashMap;/** * A connection object that has a time value, so it knows how long it has to live, * and it knows its pool, so it will automatically return to its pool if it isn't * explicitly returned. It also knows its state -- whether it is in a pool or in use. *<p> * It encapsulates a true Connection implementation, which handles all the * required JDBC methods by delegation. * <p> * PoolManConnection also sends error ConnectionEvents to its pool when encountering * any SQLException. * */public class PoolManConnection        implements PooledConnection, PooledObject {    /** The DataSource. */    private JDBCPool mypool;    /** The physical Connection. */    private Connection con;    /** Active Statements. */    private WeakHashMap openstatements;    /** ConnectionEventListeners observing this Connection. */    private ArrayList listeners;    /** Caller-side reference to this Connection. */    private PoolManConnectionHandle handle;    /** Original read state, restored upon closing */    private boolean readOnly = false;    public PoolManConnection(Connection con, ObjectPool pool) {        this.con = con;        this.mypool = (JDBCPool) pool;        this.openstatements = new WeakHashMap();        try {            if (con.isReadOnly())                this.readOnly = true;        } catch (SQLException sqle) {        }    }    public void closeAllResources() {        clean();    }    public void closeAll() {    }    public void removeOpenStatement(PoolManStatement s) {        if (openstatements.containsKey(s))            openstatements.remove(s);    }    /**     * Close any lingering Statements and ResutSets, and restore     * default values for isolation level and autocommit, committing or rolling back any     * existing unsaved changes.     */    public void clean() {        if (handle != null) {            handle.clearConnection();            handle = null;        }        // rollback any unsaved changes        try {            con.rollback();        } catch (SQLException sqle) {        }        // restore auto commit        try {            setAutoCommit(true);        } catch (SQLException sqle) {        }        // restore original iso level        try {            setTransactionIsolation(mypool.getTransactionIsolation());        } catch (SQLException sqle) {        }        // restore original read state        try {            con.setReadOnly(readOnly);        } catch (SQLException sqe) {        }        for (Iterator iter = openstatements.keySet().iterator(); iter.hasNext();) {            // automatically cleans open ResultSets as well            PoolManStatement sst = (PoolManStatement) iter.next();            sst.clean();            JDBCPool.closeStatement(sst);        }        //openstatements = new WeakHashMap();    }    /** Update the locked table in ObjectPool to prevent the object from timing out. */    public void touch() {        mypool.updateLocked(this);    }    public ObjectPool getPool() {        return this.mypool;    }    /*      POOLED CONNECTION METHODS    */    public Connection getConnection() throws SQLException {        handle = new PoolManConnectionHandle(this);        return handle;    }    public Connection getPhysicalConnection() {        return this.con;    }    /* CONNECTION EVENT METHODS */    public void addConnectionEventListener(ConnectionEventListener listener) {        if (null == this.listeners)            this.listeners = new ArrayList(1);        this.listeners.add(listener);    }    public void removeConnectionEventListener(ConnectionEventListener listener) {        try {            this.listeners.remove(listener);        } catch (Exception e) {        }    }    public void sendCloseEvent() {        // clean() sent in JDBCPool.returnConnection(), for compatibility with other API's        ConnectionEvent pev = new ConnectionEvent(this);        for (int i = 0; i < listeners.size(); i++) {            ConnectionEventListener l = (ConnectionEventListener) listeners.get(i);            l.connectionClosed(pev);        }    }    private void sendErrorEvent(SQLException sqle) {        clean();        ConnectionEvent pev = new ConnectionEvent(this, sqle);        for (int i = 0; i < listeners.size(); i++) {            ConnectionEventListener l = (ConnectionEventListener) listeners.get(i);            l.connectionErrorOccurred(pev);        }    }    /*      CONNECTION METHODS    */    public Statement createStatement()            throws SQLException {        touch();        try {            Statement s = this.con.createStatement();            PoolManStatement sst = new PoolManStatement(this, s, this.mypool);            openstatements.put(sst, null);            return sst;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public PreparedStatement prepareStatement(String sql)            throws SQLException {        touch();        try {            PreparedStatement ps = mypool.requestPooledStatement(sql);            if (ps == null) {                ps = this.con.prepareStatement(sql);            }            // when this poolman prepared statement is closed, it will return the native            // prepared statement to the pool of such statements in JBCPool.            PoolManPreparedStatement sst = new PoolManPreparedStatement(this, ps, sql, mypool);            openstatements.put(sst, null);            return sst;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public CallableStatement prepareCall(String sql) throws SQLException {        touch();        try {            CallableStatement s = this.con.prepareCall(sql);            PoolManCallableStatement sst = new PoolManCallableStatement(this, s, this.mypool);            openstatements.put(sst, null);            return sst;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public String nativeSQL(String sql) throws SQLException {        try {            String s = this.con.nativeSQL(sql);            return s;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void setAutoCommit(boolean autoCommit) throws SQLException {        try {            this.con.setAutoCommit(autoCommit);        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public boolean getAutoCommit() throws SQLException {        try {            return this.con.getAutoCommit();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void commit() throws SQLException {        try {            this.con.commit();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void rollback() throws SQLException {        try {            this.con.rollback();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    /** Close the physical Connection. */    public void close() throws SQLException {        try {            clean();            this.con.close();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public boolean isClosed() throws SQLException {        try {            return con.isClosed();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            return true;        }    }    public DatabaseMetaData getMetaData() throws SQLException {        try {            return this.con.getMetaData();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void setReadOnly(boolean readOnly) throws SQLException {        try {            this.con.setReadOnly(readOnly);        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public boolean isReadOnly() throws SQLException {        try {            return this.con.isReadOnly();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void setCatalog(String catalog) throws SQLException {        try {            this.con.setCatalog(catalog);        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public String getCatalog() throws SQLException {        try {            return this.con.getCatalog();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void setTransactionIsolation(int level) throws SQLException {        try {            this.con.setTransactionIsolation(level);        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public int getTransactionIsolation() throws SQLException {        try {            return this.con.getTransactionIsolation();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public SQLWarning getWarnings() throws SQLException {        try {            return this.con.getWarnings();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void clearWarnings() throws SQLException {        try {            this.con.clearWarnings();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public Statement createStatement(int resultSetType, int resultSetConcurrency)            throws SQLException {        touch();        try {            Statement s = null;            try {                s = this.con.createStatement(resultSetType, resultSetConcurrency);            } catch (java.lang.AbstractMethodError ae) {            }            if (s == null) {                s = this.con.createStatement();            }            PoolManStatement sst = new PoolManStatement(this, s, this.mypool);            sst.setResultSetType(resultSetType);            sst.setResultSetConcurrency(resultSetConcurrency);            openstatements.put(sst, null);            return sst;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)            throws SQLException {        touch();        try {            PoolManPreparedStatement sst = (PoolManPreparedStatement) prepareStatement(sql);            sst.setResultSetType(resultSetType);            sst.setResultSetConcurrency(resultSetConcurrency);            openstatements.put(sst, null);            return sst;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)            throws SQLException {        touch();        try {            CallableStatement s = this.con.prepareCall(sql);            PoolManCallableStatement sst = new PoolManCallableStatement(this, s, this.mypool);            sst.setResultSetType(resultSetType);            sst.setResultSetConcurrency(resultSetConcurrency);            openstatements.put(sst, null);            return sst;        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public java.util.Map getTypeMap() throws SQLException {        try {            return this.con.getTypeMap();        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }    public void setTypeMap(java.util.Map map) throws SQLException {        try {            this.con.setTypeMap(map);        } catch (SQLException sqle) {            sendErrorEvent(sqle);            throw sqle;        }    }

    /* OBJECT METHODS */

    public String toString() {        return "PoolManConnection:[Pool:" + mypool.getPoolname() + ",OpenStatements:" + openstatements.size() + "]";    }}

⌨️ 快捷键说明

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