bloritsch 01/04/06 10:25:07
Modified: src/java/org/apache/avalon/util Lock.java
src/java/org/apache/avalon/util/datasource
JdbcConnectionPool.java
Removed: src/java/org/apache/avalon/util LockException.java
Log:
Fixed Lock so that it WORKS. We had some race conditions that it was allowing.
Lock has been made lighter, and consequently faster.
Revision Changes Path
1.3 +24 -66 jakarta-avalon/src/java/org/apache/avalon/util/Lock.java
Index: Lock.java
===================================================================
RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/util/Lock.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Lock.java 2001/03/16 15:14:00 1.2
+++ Lock.java 2001/04/06 17:25:07 1.3
@@ -7,81 +7,39 @@
*/
package org.apache.avalon.util;
-import java.util.Hashtable;
-
/**
- * @author Federico Barbieri <[EMAIL PROTECTED]>
+ * A class to perform a blocking lock.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Federico Barbieri</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
*/
public class Lock
{
- private Hashtable locks = new Hashtable();
-
- public boolean isLocked( final Object key )
- {
- return (locks.get(key) != null);
- }
-
- public boolean canI( final Object key )
- {
- Object o = locks.get( key );
-
- if( null == o || o == this.getCallerId() )
- {
- return true;
- }
-
- return false;
- }
-
- public boolean lock( final Object key )
+ /**
+ * Is this locked?.
+ */
+ private boolean isLocked = false;
+
+ /**
+ * Locks.
+ */
+ public final void lock()
+ throws InterruptedException
{
- Object theLock;
-
- synchronized( this )
- {
- theLock = locks.get( key );
-
- if( null == theLock )
- {
- locks.put( key, getCallerId() );
- return true;
- }
- else if( getCallerId() == theLock )
- {
- return true;
- }
- else
- {
- return false;
- }
+ synchronized (this) {
+ while (this.isLocked) this.wait();
+ this.isLocked = true;
}
}
- public boolean unlock( final Object key )
+ /**
+ * Unlocks.
+ */
+ public final void unlock()
{
- Object theLock;
- synchronized( this )
- {
- theLock = locks.get( key );
-
- if( null == theLock )
- {
- return true;
- }
- else if( getCallerId() == theLock )
- {
- locks.remove( key );
- return true;
- }
- else
- {
- return false;
- }
+ synchronized (this) {
+ this.isLocked = false;
+ this.notify();
}
- }
-
- private Object getCallerId()
- {
- return Thread.currentThread();
}
}
1.13 +65 -49
jakarta-avalon/src/java/org/apache/avalon/util/datasource/JdbcConnectionPool.java
Index: JdbcConnectionPool.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/util/datasource/JdbcConnectionPool.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- JdbcConnectionPool.java 2001/04/06 14:43:40 1.12
+++ JdbcConnectionPool.java 2001/04/06 17:25:07 1.13
@@ -26,7 +26,7 @@
* thread to manage the number of SQL Connections.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.12 $ $Date: 2001/04/06 14:43:40 $
+ * @version CVS $Revision: 1.13 $ $Date: 2001/04/06 17:25:07 $
*/
public class JdbcConnectionPool
extends AbstractLoggable
@@ -41,7 +41,8 @@
private List m_ready = new ArrayList();
private boolean m_initialized = false;
private boolean m_disposed = false;
- private Lock m_lock = new Lock();
+ private Lock m_activeMutex = new Lock();
+ private Lock m_readyMutex = new Lock();
private Thread m_initThread;
public JdbcConnectionPool( final String url,
@@ -125,35 +126,41 @@
throw new IllegalStateException("You cannot get a Connection after the
pool is disposed");
}
- m_lock.lock(m_ready);
Poolable obj = null;
- final int size;
- if( 0 == m_ready.size() )
- {
- if (m_active.size() < m_max)
+ try {
+ this.m_readyMutex.lock();
+ final int size;
+
+ if( 0 == m_ready.size() )
{
- obj = this.createJdbcConnection();
- m_lock.lock(m_active);
- m_active.add(obj);
- m_lock.unlock(m_active);
+ if (m_active.size() < m_max)
+ {
+ obj = this.createJdbcConnection();
+
+ this.m_activeMutex.lock();
+ m_active.add(obj);
+ this.m_activeMutex.unlock();
+ }
+ else
+ {
+ throw new SQLException("There are no more Connections
available");
+ }
}
else
{
- throw new SQLException("There are no more Connections available");
- }
- }
- else
- {
- obj = (Poolable)m_ready.remove( 0 );
+ obj = (Poolable)m_ready.remove( 0 );
- m_lock.lock(m_active);
- m_active.add( obj );
- m_lock.unlock(m_active);
+ this.m_activeMutex.lock();
+ m_active.add( obj );
+ this.m_activeMutex.unlock();
+ }
+ } catch (Exception e) {
+ getLogger().debug("JdbcConnectionPool.get()", e);
+ } finally {
+ this.m_readyMutex.unlock();
}
- m_lock.unlock(m_ready);
-
if (((Connection)obj).getAutoCommit() != m_autoCommit) {
((Connection)obj).setAutoCommit(m_autoCommit);
}
@@ -171,10 +178,10 @@
throw new IllegalStateException("You cannot return an object to an
uninitialized pool");
}
- m_lock.lock(m_active);
- m_active.remove( obj );
-
try {
+ this.m_activeMutex.lock();
+ m_active.remove( obj );
+
if(! m_disposed)
{
JdbcConnection connection = (JdbcConnection) obj;
@@ -185,49 +192,58 @@
connection = this.createJdbcConnection();
}
- m_lock.lock(m_ready);
+ this.m_readyMutex.lock();
m_ready.add( connection );
- m_lock.unlock(m_ready);
+ this.m_readyMutex.unlock();
} else {
recycle((Recyclable) obj);
}
- } catch (SQLException se) {
- getLogger().warn("Error returning connection to pool", se);
+ } catch (Exception e) {
+ getLogger().warn("Error returning connection to pool", e);
+ } finally {
+ this.m_activeMutex.unlock();
}
- m_lock.unlock(m_active);
-
getLogger().debug( "JdbcConnection '" + m_dburl + "' has been returned to
the pool." );
}
public void run()
{
- m_lock.lock(m_ready);
- for (int i = 0; i < m_max; i++)
- {
- try {
- m_ready.add( createJdbcConnection() );
- } catch (SQLException se) {
- getLogger().error( "Could not create connection to database", se );
+ try {
+ this.m_readyMutex.lock();
+ for (int i = 0; i < m_max; i++)
+ {
+ try {
+ m_ready.add( createJdbcConnection() );
+ } catch (SQLException se) {
+ getLogger().error( "Could not create connection to database",
se );
+ }
}
- }
- if (m_ready.size() > 0) {
- m_initialized = true;
+ if (m_ready.size() > 0) {
+ m_initialized = true;
+ }
+ } catch (Exception e) {
+ getLogger().debug("JdbcConnectionPool.run()", e);
+ } finally {
+ this.m_readyMutex.unlock();
}
- m_lock.unlock(m_ready);
}
public void dispose()
{
- m_lock.lock(m_ready);
- m_disposed = true;
+ try {
+ this.m_readyMutex.lock();
+ m_disposed = true;
- while( ! m_ready.isEmpty() )
- {
- recycle( (Recyclable)m_ready.remove( 0 ) );
+ while( ! m_ready.isEmpty() )
+ {
+ recycle( (Recyclable)m_ready.remove( 0 ) );
+ }
+ } catch (Exception e) {
+ getLogger().debug("JdbcConnectionPool.dispose()", e);
+ } finally {
+ this.m_readyMutex.unlock();
}
-
- m_lock.unlock(m_ready);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]