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

📄 connectionpoolmanager.java

📁 JSP聊天系统
💻 JAVA
字号:
package org.ehotsoft.yekki.sql.pool;import java.sql.*;import java.util.*;public class ConnectionPoolManager implements Driver, Runnable {	public static final String URL_PREFIX = "jdbc:yekki:pool:";	public static final int MAJOR_VERSION = 1;	public static final int MINOR_VERSION = 0;	private Hashtable aliasHash;	private long sleepInterval;	private boolean trace;	public ConnectionPoolManager() throws SQLException {		aliasHash = new Hashtable();		DriverManager.registerDriver( this );		trace = false;	}	public ConnectionPoolManager( int monitorInterval ) throws SQLException {		this();		this.sleepInterval = monitorInterval * 1000;				Thread t = new Thread( this );		t.setDaemon( true );		t.start();	}	 public void addAlias( String alias, String driver, String url, String username,				String password, int maxConn, int idleTimeout, int checkoutTimeout  ) 		throws ClassNotFoundException, InstantiationException, IllegalAccessException {		addAlias( alias, driver, url, username, password, maxConn, idleTimeout, checkoutTimeout, 0 );	} 	public void addAlias( String alias, String driver, String url, String username, String password,			   int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout )		throws ClassNotFoundException, InstantiationException, IllegalAccessException {				addAlias( alias, driver, url, username, password, maxConn, idleTimeout, checkoutTimeout, maxCheckout, true );	}  	public void addAlias( String alias, String driver, String url, String username, String password,			   int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout, boolean cacheStatements )		throws ClassNotFoundException, InstantiationException, IllegalAccessException {				Class.forName(driver).newInstance();		ConnectionPool pool = new ConnectionPool( alias, url, username, password, 						 maxConn, idleTimeout, checkoutTimeout, maxCheckout );		pool.setCacheStatements( cacheStatements );		addAlias( pool );	}	public void addAlias(String alias, String driver, String url, String username, String password,			   int maxConn, int idleTimeout, int checkoutTimeout, int maxCheckout, int rowPrefetch)		throws ClassNotFoundException, InstantiationException, IllegalAccessException {	  		addAlias(alias, driver, url, username, password, maxConn,			idleTimeout, checkoutTimeout, maxCheckout, rowPrefetch, true);	}	public void addAlias( String alias, String driver, String url, String username, String password,			int maxConn, int idleTimeout, int checkoutTimeout,			int maxCheckout, int rowPrefetch,boolean cacheStatements )		throws ClassNotFoundException, InstantiationException, IllegalAccessException {		Class.forName( driver ).newInstance();		ConnectionPool pool = new ConnectionPool( alias, url, username, password,						 maxConn, idleTimeout, checkoutTimeout, maxCheckout );		pool.setCacheStatements( cacheStatements );		pool.setPrefetchSize( rowPrefetch );		addAlias( pool );	}  	public synchronized void addAlias( ConnectionPool pool ) {		aliasHash.put(pool.getAlias(), pool);	}	public synchronized void removeAlias( String alias )		throws SQLException {				ConnectionPool p = getPool( alias );		aliasHash.remove(alias);		p.removeAllConnections();	}	public Enumeration getPools() {		return aliasHash.elements();	}	public ConnectionPool getPool(String alias) throws SQLException {		ConnectionPool p = ( ConnectionPool )aliasHash.get(alias);				if( p == null ) {						throw new SQLException( "No connection pool created for alias: " + alias );		}		return p;	}  	public void run() {		while( true ) {			try {				Thread.currentThread().sleep( sleepInterval );			}			catch( InterruptedException e ) { }			for ( Enumeration enum = aliasHash.elements(); enum.hasMoreElements(); ) {				ConnectionPool pool = ( ConnectionPool )enum.nextElement();								try {					pool.reapIdleConnections();				}				catch( Exception e ) {					e.printStackTrace();				}			}		}	}	public void setTracing( boolean on ) {		if ( on != trace ) {			for ( Enumeration e = getPools(); e.hasMoreElements(); ) {				ConnectionPool p = (ConnectionPool)e.nextElement();				p.setTracing(on);			}			String str = "off";			if( on ) str = "on";			System.err.println( "ConnectionPoolManager: Tracing turned " + str );			trace = on;		}	}	public String dumpInfo() {		String LS = System.getProperty( "line.separator" );		Enumeration allPools = getPools();		ConnectionPool currentPool = null;		StringBuffer report = new StringBuffer();		while (allPools.hasMoreElements()) {		  currentPool = ( ConnectionPool )allPools.nextElement();		  report.append( currentPool.dumpInfo() );		}		return report.toString();	}	//implements Driver infterface	public Connection connect( String url, Properties props ) throws SQLException {			if ( !url.startsWith( URL_PREFIX ) ) {						return null;		}		if ( url.length() <= URL_PREFIX.length() ) {			throw new SQLException("Invalid URL: " + url + " -- No alias given");		}		String alias = url.substring( URL_PREFIX.length() );		if ( trace ) System.err.println("ConnectionPoolManager: connect() called for " + alias + ".  calling pool.getConnection()");		ConnectionPool p = getPool( alias );		return p.getConnection();	}  	public boolean acceptsURL( String url ) {		return url.startsWith( URL_PREFIX );	}	public DriverPropertyInfo[] getPropertyInfo( String str, Properties props ) {		return new DriverPropertyInfo[ 0 ];	} 	public boolean jdbcCompliant() {		return false;	}	public int getMajorVersion() {				return MAJOR_VERSION;	}	public int getMinorVersion() {				return MINOR_VERSION;	}}

⌨️ 快捷键说明

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