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

📄 connectionproxy.java

📁 羽量级数据持久层开发框架
💻 JAVA
字号:
package org.speedframework.connection;

//~--- JDK imports ------------------------------------------------------------

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import java.sql.Connection;
import java.sql.SQLException;

/**
 *  类描述信息,描述类的主要职责和用处。
 *
 *
 * @version    $LastChangedRevision: 1945 $, 2007.09.29 at 02:14:26 CST
 * @author     <a href="mailto:falcon8848@gmail.com">piginzoo </a>
 */
public class ConnectionProxy implements InvocationHandler
 {

    /** 属性描述信息 */
    private final static String CLOSE_METHOD_NAME = "close";

    /** 属性描述信息 */
    private Connection conn = null;

    /** 属性描述信息 */
    private String connectionFactoryId = "";

    // ????????

    /** 属性描述信息 */
    private boolean inUse = false;

    // ????????锟斤拷????l??????????

    /** 属性描述信息 */
    private long lastAccessTime = System.currentTimeMillis();

    /**
     * Constructs ...
     *
     *
     * @param conn
     * @param inUse
     * @param connectionFactoryId
     */
    public ConnectionProxy(Connection conn, boolean inUse, String connectionFactoryId) {
        this.conn                = conn;
        this.inUse               = inUse;
        this.connectionFactoryId = connectionFactoryId;
    }

    /**
     * Returns the conn.
     *
     * @return Connection
     */
    public Connection getConnection() {

        // ????????l??conn???????????close????
        Class[]    inter = { java.sql.Connection.class };
        Connection conn2 = null;

        conn2 = (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(), inter, this);

        return conn2;
    }

    /**
     * ?锟斤拷?????????????????l??
     *
     * @throws SQLException
     */
    void close() throws SQLException {

        // ??????????conn????锟斤拷?????l?????????????close????????????l??
        conn.close();
    }

    /**
     * Returns the inUse.
     *
     * @return boolean
     */
    public boolean isInUse() {
        return inUse;
    }

    /**
     * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
     * java.lang.reflect.Method,java.lang.Object)
     *
     * @param proxy
     * @param m
     * @param args
     *
     * @return
     *
     * @throws Throwable
     */
    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
        Object obj = null;

        // ?锟斤拷?????????close?????????????close???????l???????????
        if (CLOSE_METHOD_NAME.equals(m.getName())) {
            synchronized (ConnectionAdaptorPool.getPoolById(connectionFactoryId)) {
                setInUse(false);
                ConnectionAdaptorPool.getPoolById(connectionFactoryId).releaseAdaptor();
            }
        } else {
            obj = m.invoke(conn, args);
        }

        // ?????????锟斤拷????????????????l??
        lastAccessTime = System.currentTimeMillis();

        return obj;
    }

    /**
     * Returns the lastAccessTime.
     *
     * @return long
     */
    public long getLastAccessTime() {
        return lastAccessTime;
    }

    /**
     * Sets the inUse.
     *
     * @param inUse The inUse to set
     */
    public void setInUse(boolean inUse) {
        this.inUse = inUse;
    }
}

⌨️ 快捷键说明

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