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

📄 pooledconnection.java

📁 JSP聊天系统
💻 JAVA
字号:
package org.ehotsoft.yekki.sql.pool;import java.io.InputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Statement;import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;public class PooledConnection	implements Connection, Runnable {	private ConnectionPool pool;	private Connection conn;	private boolean locked;	private long lastAccess;	private long lastCheckin;	private int checkoutCount;	private PooledStatement theStatement = null;	int totalStatements;	int preparedCalls;	int preparedStatementHits;	int preparedStatementMisses;	int preparedStatements;	private Exception traceException;	private static final int closedConnMaxRetry = 5;	private static final int closedConnRetryWait = 1000;	private Map prepStmts;	public PooledConnection( Connection conn, ConnectionPool pool ) {		this.conn = conn;		this.pool = pool;		this.locked = false;		this.lastAccess = 0;		this.checkoutCount = 0;		this.totalStatements = 0;		prepStmts = new HashMap();	}	public void run() {		try {			closeStatements();		}		catch( SQLException e ) {			e.printStackTrace();		}		try {			getConnection().close();		}		catch( SQLException e ) {			e.printStackTrace();		}	}	public void closeStatements() throws SQLException {		if ( theStatement != null ) {			theStatement.getStatement().close();		}		for ( Iterator it = prepStmts.values().iterator(); it.hasNext(); ) {						try {				PooledPreparedStatement stmt =  ( PooledPreparedStatement )it.next();				stmt.getStatement().close();			}			catch ( SQLException e ) {				e.printStackTrace();			}		}	}	public synchronized boolean getLock() {		if( locked ) {			return false;		}		else {			locked = true;			checkoutCount++;			lastAccess = System.currentTimeMillis();			return true;		}	}	public boolean isLocked() {		return locked;	}	public int getCheckoutCount() {		return checkoutCount;	}	public long getLastAccess() {		return lastAccess;	}	public long getLastCheckin() {		return lastCheckin;	}	public void close() throws SQLException {		lastAccess = System.currentTimeMillis();		pool.returnConnection( this );	}	protected void releaseLock() {		lastCheckin = System.currentTimeMillis();		locked = false;	}	protected Connection getConnection() {		return conn;	}	public Connection getNativeConnection() {		return conn;	}/*** Dump some information about this connection and the statement**/	public String dumpInfo() {			String LS = System.getProperty( "line.separator" );				StringBuffer report = new StringBuffer();		report.append( "\t\tConnection: " )				.append( this.toString() )				.append( LS )				.append( "\t\t\tStatements Requested: " )				.append( this.totalStatements )				.append( LS )				.append( "\t\t\tPrepared Calls: " )				.append( this.preparedCalls )				.append( LS )				.append( "\t\t\tPrepared Statements Hits: " )				.append( this.preparedStatementHits )				.append( LS )				.append( "\t\t\tPrepared Statements Misses: " )				.append( this.preparedStatementMisses )				.append( LS )				.append( "\t\t\tCheckout count: " )				.append( this.getCheckoutCount() )				.append( LS )				.append( "\t\t\tLast Checkout: " )				.append( getLastAccess() )				.append( ": " )				.append( new java.util.Date( this.getLastAccess() ) )				.append( LS )				.append( "\t\t\tLast Checkin : " )				.append( getLastCheckin() )				.append(  ": " )				.append( new java.util.Date( getLastCheckin() ) )				.append( LS )				.append( "\t\t\t" )				.append( ( isLocked() ? "Connection IS checked out." : "Connection is NOT checked out." ) )				.append( LS )				.append( "\t\t\tCheckout Stack Trace: " );		if ( traceException != null ) {			StringWriter sw = new StringWriter();			PrintWriter pw = new PrintWriter( sw );			traceException.printStackTrace( pw );			report.append( sw.toString() );		}		else {			report.append( "null" );		}		report.append( LS );		if ( theStatement!=null ) {						report.append( theStatement.dumpInfo() );		}		return report.toString();	}	public void guardConnection() {				boolean badConnection = false;		try {		  badConnection  = conn.isClosed();		}		catch ( SQLException e ) {		  			badConnection = true;		}				if ( badConnection ) {			pool.numConnectionFaults++;			System.err.println( "PooledConnection.guardConnection(): " + "found closed Connection. " + "Statement information follows. Attempting to recover." );			if ( theStatement != null )				System.err.println( "PooledConnection.guardConnection(): " + theStatement.dumpInfo() );			else				System.err.println( "PooledConnection.guardConnection: statement was null" ); 			this.theStatement = null;			int retryCount = 0;	  			for ( retryCount = 0 ; retryCount < closedConnMaxRetry; retryCount++ ) {								try {									conn = pool.createDriverConnection();				}				catch ( SQLException se ) {					System.err.println( "PooledConnection.guardConnection(): " + "failed to create connection on try #" + retryCount );										try {												Thread.sleep( closedConnRetryWait );					}					catch ( InterruptedException ie ) {						System.err.println( "PooledConnection: " + ie );					}					continue;         				}								System.err.println( "PooledConnection.guardConnection(): " + "Recovered connection" );								return;      			}		}	}	public Statement createStatement() throws SQLException {		guardConnection();		this.totalStatements++;				if ( pool.getCacheStatements() ) {			if ( theStatement == null ) {				theStatement = new PooledStatement( conn.createStatement() );			}			return theStatement;		}		else {						return conn.createStatement();		}	}	public Map getTypeMap() throws SQLException {				return conn.getTypeMap();	}	public PreparedStatement prepareStatement( String sql ) throws SQLException {				if ( pool.getCacheStatements() ) {			PreparedStatement stmt = ( PreparedStatement )prepStmts.get( sql );						if ( stmt == null ) {				stmt = conn.prepareStatement( sql );				stmt = new PooledPreparedStatement( stmt );			   				synchronized ( prepStmts ) {					prepStmts.put( sql, stmt );				}				preparedStatementMisses++;			}			else {			  preparedStatementHits++;			}			return stmt;		}		else {						preparedStatements++;			return conn.prepareStatement( sql );		}	}	public PreparedStatement prepareStatement( String sql, int resultSetType, int resultSetConcurrency )		throws SQLException {		if ( pool.getCacheStatements() ) {			preparedStatementMisses++;		}		else {			preparedStatements++;		}		PreparedStatement stmt = conn.prepareStatement( sql, resultSetType, resultSetConcurrency );				return stmt;	}	public CallableStatement prepareCall( String sql )		throws SQLException {		preparedCalls++;		CallableStatement stmt =  conn.prepareCall( sql );				return stmt;	}	public CallableStatement prepareCall( String sql, int resultSetType, int resultSetConcurrency )		throws SQLException {				preparedCalls++;          		CallableStatement stmt = conn.prepareCall( sql, resultSetType, resultSetConcurrency );				return stmt;	}	public Statement createStatement( int resultSetType, int resultSetConcurrency )	  throws SQLException {		this.totalStatements++;		return conn.createStatement( resultSetType, resultSetConcurrency );	}	public String nativeSQL( String sql ) throws SQLException {		return conn.nativeSQL( sql );	}	public void setAutoCommit( boolean autoCommit ) throws SQLException {		conn.setAutoCommit( autoCommit );	}	public boolean getAutoCommit() throws SQLException {		return conn.getAutoCommit();	}	public void commit() throws SQLException {		conn.commit();	}	public void rollback() throws SQLException {		conn.rollback();	}	public boolean isClosed() throws SQLException {		return conn.isClosed();	}	public DatabaseMetaData getMetaData() throws SQLException {		return conn.getMetaData();	}	public void setReadOnly( boolean readOnly ) throws SQLException {		conn.setReadOnly( readOnly );	}	public boolean isReadOnly() throws SQLException {		return conn.isReadOnly();	}	public void setCatalog( String catalog ) throws SQLException {		conn.setCatalog( catalog );	}	public String getCatalog() throws SQLException {		return conn.getCatalog();	}	public void setTypeMap( Map map ) throws SQLException {		conn.setTypeMap( map );	}	public void setTransactionIsolation( int level ) throws SQLException {		conn.setTransactionIsolation( level );	}	public int getTransactionIsolation() throws SQLException {		return conn.getTransactionIsolation();	}	public SQLWarning getWarnings() throws SQLException {		return conn.getWarnings();	}	public void clearWarnings() throws SQLException {		conn.clearWarnings();	}	protected void setTraceException( Exception e ) {		traceException = e;	}	public Exception getTraceException() {		return traceException;	}}

⌨️ 快捷键说明

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