1. Link: http://www.javahispano.org/contenidos/es/pool_de_objetos_ii/
Y EN LOS COMENTARIOS jmarranz propuso algo que no me quedo muy claro...
2. Es necesario crear un servlet para poder darle uso correcto a esta clase?
3. Implemente el Main y la conexión, pero lo normal, o el paso siguiente, es que uno cree un Statement a partir del objeto que en este caso seria obj
Asi que deberia ser:
Statement obj = conexion.createStatement();
String UserQuery = "SELECT * FROM usuarios";
Pero la función es tipo object:
No se como finaliza para obtener el cursor final de conexión?
JDBCConnectionPool.java
Código:
ObjectPool.javaimport java.sql.DriverManager; import java.sql.SQLException; import java.sql.Connection; import java.sql.*; public class JDBCConnectionPool extends ObjectPool{ private String dsn, usr, pwd; public static void main(String[] args){ String url = "jdbc:sybase:Tds:VISTAESCRITORIO:5000/Pruebas"; String Driver = "com.sybase.jdbc3.jdbc.SybDriver"; JDBCConnectionPool obj = new JDBCConnectionPool(Driver,url,"sa","1234567"); } public JDBCConnectionPool( String driver, String dsn, String usr, String pwd ) { try { Class.forName( driver ).newInstance(); } catch( Exception e ) { e.printStackTrace(); } this.dsn = dsn; this.usr = usr; this.pwd = pwd; } Object create() throws SQLException { return( DriverManager.getConnection( dsn, usr, pwd ) ); } boolean validate( Object o ) { try { return( ! ( ( Connection ) o ).isClosed() ); } catch( SQLException e ) { e.printStackTrace(); return false; } } void expire( Object o ) { try { ( ( Connection ) o ).close(); } catch( SQLException e ) { e.printStackTrace(); } } public Connection borrowConnection() throws SQLException { try { return( ( Connection ) super.checkOut() ); } catch( Exception e ) { throw( (SQLException) e ); } } public void returnConnection( Connection c ) { super.checkIn( c ); } }
Código:
// Copyright (c) 1998 Thomas E. Davis import java.util.Enumeration; import java.util.Hashtable; public abstract class ObjectPool { private long expirationTime; private long lastCheckOut; private Hashtable locked; private Hashtable unlocked; private CleanUpThread cleaner; ObjectPool() { expirationTime = ( 1000 * 60 ); // 60 seconds locked = new Hashtable(); unlocked = new Hashtable(); lastCheckOut = System.currentTimeMillis(); cleaner = new CleanUpThread( this, expirationTime ); cleaner.start(); } abstract Object create() throws Exception; abstract boolean validate( Object o ); abstract void expire( Object o ); synchronized Object checkOut() throws Exception { long now = System.currentTimeMillis(); lastCheckOut = now; Object o; if( unlocked.size() > 0 ) { Enumeration e = unlocked.keys(); while( e.hasMoreElements() ) { o = e.nextElement(); if( validate( o ) ) { unlocked.remove( o ); locked.put( o, new Long( now ) ); return( o ); } else { unlocked.remove( o ); expire( o ); o = null; } } } o = create(); locked.put( o, new Long( now ) ); return( o ); } synchronized void checkIn( Object o ) { if( o != null ) { locked.remove( o ); unlocked.put( o, new Long( System.currentTimeMillis() ) ); } } synchronized void cleanUp() { Object o; long now = System.currentTimeMillis(); Enumeration e = unlocked.keys(); while( e.hasMoreElements() ) { o = e.nextElement(); if( ( now - ( ( Long ) unlocked.get( o ) ).longValue() ) > expirationTime ) { unlocked.remove( o ); expire( o ); o = null; } } System.gc(); } } class CleanUpThread extends Thread { private ObjectPool pool; private long sleepTime; CleanUpThread( ObjectPool pool, long sleepTime ) { this.pool = pool; this.sleepTime = sleepTime; } public void run() { while( true ) { try { sleep( sleepTime ); } catch( InterruptedException e ) { // ignore it } pool.cleanUp(); } } }