bloritsch 01/11/02 11:28:35 Modified: src/java/org/apache/avalon/excalibur/datasource JdbcDataSource.java JdbcConnectionPool.java Log: First stab at blocking code Revision Changes Path 1.11 +18 -3 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/datasource/JdbcDataSource.java Index: JdbcDataSource.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/datasource/JdbcDataSource.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- JdbcDataSource.java 2001/11/02 18:57:11 1.10 +++ JdbcDataSource.java 2001/11/02 19:28:35 1.11 @@ -34,7 +34,7 @@ * </pre> * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> - * @version CVS $Revision: 1.10 $ $Date: 2001/11/02 18:57:11 $ + * @version CVS $Revision: 1.11 $ $Date: 2001/11/02 19:28:35 $ * @since 4.0 */ public class JdbcDataSource @@ -67,6 +67,7 @@ final int min = controller.getAttributeAsInteger( "min", 1 ); final int max = controller.getAttributeAsInteger( "max", 3 ); + final long timeout = controller.getAttributeAsLong( "timeout", -1 ); final boolean autoCommit = configuration.getChild("auto-commit").getValueAsBoolean(true); final boolean oradb = controller.getAttributeAsBoolean( "oradb", false ); final String connectionClass = controller.getAttribute( "connection-class", null ); @@ -160,7 +161,8 @@ try { m_pool = new JdbcConnectionPool( factory, poolController, l_min, l_max, autoCommit ); - m_pool.setLogger(getLogger()); + m_pool.setLogger( getLogger() ); + m_pool.setTimeout( timeout ); m_pool.initialize(); } catch (Exception e) @@ -179,7 +181,20 @@ public Connection getConnection() throws SQLException { - try { return (Connection) m_pool.get(); } + try + { + return (Connection) m_pool.get(); + } + catch( final SQLException se ) + { + if (getLogger().isWarnEnabled()) + { + getLogger().warn( "Could not return Connection", se ); + } + + // Rethrow so that we keep the original stack trace + throw se; + } catch( final Exception e ) { if (getLogger().isWarnEnabled()) 1.7 +63 -2 jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/datasource/JdbcConnectionPool.java Index: JdbcConnectionPool.java =================================================================== RCS file: /home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/datasource/JdbcConnectionPool.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- JdbcConnectionPool.java 2001/09/26 17:52:28 1.6 +++ JdbcConnectionPool.java 2001/11/02 19:28:35 1.7 @@ -21,7 +21,7 @@ * thread to manage the number of SQL Connections. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> - * @version CVS $Revision: 1.6 $ $Date: 2001/09/26 17:52:28 $ + * @version CVS $Revision: 1.7 $ $Date: 2001/11/02 19:28:35 $ * @since 4.0 */ public class JdbcConnectionPool @@ -31,6 +31,7 @@ private Thread m_initThread; private final boolean m_autoCommit; private boolean m_noConnections = false; + private long m_wait = -1; public JdbcConnectionPool( final JdbcConnectionFactory factory, final DefaultPoolController controller, final int min, final int max, final boolean autoCommit) throws Exception @@ -41,6 +42,22 @@ this.m_autoCommit = autoCommit; } + /** + * Set the timeout in milliseconds for blocking when waiting for a + * new connection. It defaults to -1. Any number below 1 means that there + * is no blocking, and the Pool fails hard. Any number above 0 means we + * will wait for that length of time before failing. + */ + public void setTimeout( long timeout ) + { + if (this.m_initialized) + { + throw new IllegalStateException("You cannot change the timeout after the pool is initialized"); + } + + m_wait = timeout; + } + public void initialize() { m_initThread = new Thread( this ); @@ -49,7 +66,45 @@ protected final Poolable newPoolable() throws Exception { - JdbcConnection conn = (JdbcConnection) super.newPoolable(); + JdbcConnection conn = null; + + if ( m_wait < 1 ) + { + conn = (JdbcConnection) super.newPoolable(); + } + else + { + long curMillis = new Date().getTime(); + long endTime = curMillis + m_wait; + + while ( ( null == conn ) && ( curMillis < endTime ) ) + { + try + { + m_mutex.unlock(); + this.wait( m_wait ); // wait for 50 millis before trying again + } + finally + { + m_mutex.lock(); + } + + try + { + conn = (JdbcConnection) super.newPoolable(); + } + finally + { + // Do nothing except keep waiting + } + } + } + + if (null == conn ) + { + throw new NoAvailableConnectionException("All available connections are in use"); + } + conn.setPool(this); return conn; } @@ -119,6 +174,12 @@ } return obj; + } + + public void put( Poolable obj ) + { + super.put( obj ); + this.notifyAll(); } public void run()
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>