📄 proxyconn.java
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的 */package biz.tbuy.common.pool;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;/** * @version 1.0 * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class ProxyConn { private boolean _busy; private long _createTime; // 该代理连接的创建时间,主要用于检查超时时间 private long _lastTime; // 最后一次状态改变的时间 private Connection _conn; private Statement _statement; private PreparedStatement _ps; public ProxyConn(Connection conn) { _conn = conn; _createTime = System.currentTimeMillis(); _lastTime = System.currentTimeMillis(); } /** * 获取实际的Connection * @return conn */ public Connection getConn() { return _conn; } /** * 设置连接的繁忙状态 * @param busy - boolean */ public void setBusy(boolean busy) { _busy = busy; this.setLastTime(System.currentTimeMillis()); } /** * 判断该连接是否处于繁忙状态 * @return true 如果处于繁忙, 否则false */ public boolean isBusy() { return _busy; } /** * 判断该连接是否已经处于无效状态, * 如果为null, isClose...都视为无效 */ public boolean isClosed() { try { if (_conn == null || _conn.isClosed()) { return true; } } catch (SQLException ex) {} return false; } /** * 设置最后一次状态改变的时间 * @param lastTime System.currentTimeMillis(); */ public void setLastTime(long lastTime) { _lastTime = lastTime; } /** * 获得最后一次状态改变的时间 * @return long */ public long getLastTime() { return _lastTime; } /** * 获取该代理连接存在于某一状态下的毫秒数, * 如“空闲”或“繁忙” * @return stateTime */ public long getStateTime() { return System.currentTimeMillis() - _lastTime; } /** * 获取该代理连接已经存在的毫秒数 * @return liveTime long数据 */ public long getLiveTime() { return System.currentTimeMillis() - _createTime; } /** * Statement; * @return statement * @throws java.sql.SQLException */ public Statement createStatement() throws SQLException { _statement = _conn.createStatement(); return _statement; } /** * @param sql * @return PreparedStatement; * @throws java.sql.SQLException */ public PreparedStatement prepareStatement(String sql) throws SQLException { _ps = _conn.prepareStatement(sql); return _ps; } /** * 释放连接,并不真正释放Connection, * 如果成功,则将该代理连接设为空闲状态. */ public void close() { try { if (_statement != null) { _statement.close(); } if (_ps != null) { _ps.close(); } this.setBusy(false); } catch (SQLException sqle) { this.setBusy(true); } } /** * 彻底关闭连接 */ public void realClose() { try { this.close(); _conn.close(); } catch (SQLException ex) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -