You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ol...@apache.org on 2003/05/18 17:10:49 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/otm/lock/wait TimeoutStrategy.java NoWaitStrategy.java
olegnitz 2003/05/18 08:10:49
Modified: src/java/org/apache/ojb/otm/kit SimpleKit.java
src/java/org/apache/ojb/otm/lock ObjectLock.java
src/java/org/apache/ojb/otm/lock/wait NoWaitStrategy.java
Added: src/java/org/apache/ojb/otm/lock/wait TimeoutStrategy.java
Log:
Timeout strategy of waiting for lock implemented.
Revision Changes Path
1.6 +13 -13 db-ojb/src/java/org/apache/ojb/otm/kit/SimpleKit.java
Index: SimpleKit.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/kit/SimpleKit.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SimpleKit.java 18 May 2003 09:49:10 -0000 1.5
+++ SimpleKit.java 18 May 2003 15:10:49 -0000 1.6
@@ -64,7 +64,7 @@
import org.apache.ojb.otm.lock.map.InMemoryLockMap;
import org.apache.ojb.otm.lock.map.LockMap;
import org.apache.ojb.otm.lock.wait.LockWaitStrategy;
-import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
+import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
import org.apache.ojb.otm.swizzle.NoSwizzling;
import org.apache.ojb.otm.swizzle.Swizzling;
import org.apache.ojb.otm.transaction.LocalTransactionFactory;
@@ -82,13 +82,13 @@
private static SimpleKit _instance;
- private TransactionFactory _txFactory;
- private Swizzling _swizzlingStrategy;
- private LockWaitStrategy _lockWaitStrategy;
- private LockMap _lockMap;
- private ObjectCopyStrategy _copyStrategy;
- private ObjectStore _cacheStore;
- private GlobalCache _globalCache;
+ protected TransactionFactory _txFactory;
+ protected Swizzling _swizzlingStrategy;
+ protected LockWaitStrategy _lockWaitStrategy;
+ protected LockMap _lockMap;
+ protected ObjectCopyStrategy _copyStrategy;
+ protected ObjectStore _cacheStore;
+ protected GlobalCache _globalCache;
/**
* Constructor for SimpleKit.
@@ -98,7 +98,7 @@
super();
_txFactory = new LocalTransactionFactory();
_swizzlingStrategy = new NoSwizzling();
- _lockWaitStrategy = new NoWaitStrategy();
+ _lockWaitStrategy = new TimeoutStrategy();
_lockMap = new InMemoryLockMap();
_copyStrategy = new SerializeObjectCopyStrategy();
_cacheStore = new HashMapObjectStore();
@@ -121,7 +121,7 @@
/**
* @see org.apache.ojb.otm.OTMKit#getTransactionFactory()
*/
- protected TransactionFactory getTransactionFactory ()
+ protected TransactionFactory getTransactionFactory()
{
return _txFactory;
}
@@ -129,7 +129,7 @@
/**
* @see org.apache.ojb.otm.OTMKit#getSwizzlingStrategy()
*/
- public Swizzling getSwizzlingStrategy ()
+ public Swizzling getSwizzlingStrategy()
{
return _swizzlingStrategy;
}
@@ -138,7 +138,7 @@
/**
* @see org.apache.ojb.otm.OTMKit#getLockFactory()
*/
- public LockWaitStrategy getLockWaitStrategy ()
+ public LockWaitStrategy getLockWaitStrategy()
{
return _lockWaitStrategy;
}
@@ -146,7 +146,7 @@
/**
* @see org.apache.ojb.otm.OTMKit#getLockMap()
*/
- public LockMap getLockMap ()
+ public LockMap getLockMap()
{
return _lockMap;
}
1.5 +35 -35 db-ojb/src/java/org/apache/ojb/otm/lock/ObjectLock.java
Index: ObjectLock.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/lock/ObjectLock.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ObjectLock.java 18 May 2003 09:49:10 -0000 1.4
+++ ObjectLock.java 18 May 2003 15:10:49 -0000 1.5
@@ -88,7 +88,7 @@
// Constructor
////////////////////////////////////////
- public ObjectLock (Identity oid)
+ public ObjectLock(Identity oid)
{
_oid = oid;
}
@@ -97,51 +97,54 @@
// Operations
////////////////////////////////////////
- public Identity getTargetIdentity ()
+ public Identity getTargetIdentity()
{
return _oid;
}
- public Transaction getWriter ()
+ public Transaction getWriter()
{
return (_writer == null)? null: _writer.getTx();
}
- public boolean isReader (Transaction tx)
+ public boolean isReader(Transaction tx)
{
return _readers.containsKey(tx);
}
- public boolean doesReaderExists ()
+ public boolean doesReaderExists()
{
return (_readers.size() > 0);
}
- public Collection getReaders ()
+ public Collection getReaders()
{
return Collections.unmodifiableCollection(_readers.keySet());
}
- public void readLock (Transaction tx)
+ public void readLock(Transaction tx)
{
if (!isReader(tx)) {
new LockEntry(tx);
}
}
- public void writeLock (Transaction tx)
+ public void writeLock(Transaction tx)
throws LockingException
{
- LockEntry lock = (LockEntry)_readers.get(tx);
- if (lock == null)
+ if (getWriter() != tx)
{
- lock = new LockEntry(tx);
- }
+ LockEntry lock = (LockEntry) _readers.get(tx);
- lock.writeLock();
+ if (lock == null)
+ {
+ lock = new LockEntry(tx);
+ }
+ lock.writeLock();
+ }
}
- public void releaseLock (Transaction tx)
+ public void releaseLock(Transaction tx)
{
LockEntry lock = (LockEntry)_readers.get(tx);
if (lock != null)
@@ -150,7 +153,7 @@
}
}
- public void waitForTx (Transaction tx)
+ public void waitForTx(Transaction tx)
throws LockingException
{
OTMKit kit = tx.getKit();
@@ -167,7 +170,7 @@
public Transaction _tx;
public ArrayList _listeners;
- public LockEntry (Transaction tx)
+ public LockEntry(Transaction tx)
{
_tx = tx;
_listeners = null;
@@ -181,7 +184,7 @@
* @return ArrayList of LockListeners
*
*/
- public ArrayList getListeners ()
+ public ArrayList getListeners()
{
return _listeners;
}
@@ -193,7 +196,7 @@
* @return Transaction
*
*/
- public Transaction getTx ()
+ public Transaction getTx()
{
return _tx;
}
@@ -206,7 +209,7 @@
* @param listener the LockListener
*
*/
- public void addListener (LockListener listener)
+ public void addListener(LockListener listener)
{
if (listener != null)
{
@@ -225,30 +228,27 @@
* the lock is released by the writer.
*
*/
- public void writeLock ()
+ public void writeLock()
throws LockingException
{
- synchronized (ObjectLock.this)
- {
- if (_writer == null)
+ while (true) {
+ if (_writer != null && _writer._tx != _tx)
{
- _writer = this;
+ waitForTx(_writer.getTx());
}
- }
- if (_writer == null)
- {
- writeLock();
- }
- else if (_writer != this)
- {
- waitForTx(_writer.getTx());
- writeLock();
+ synchronized (ObjectLock.this)
+ {
+ if (_writer == null || _writer._tx == _tx)
+ {
+ _writer = this;
+ return;
+ }
+ }
}
-
}
- public void release ()
+ public void release()
{
synchronized (ObjectLock.this)
{
1.2 +9 -9 db-ojb/src/java/org/apache/ojb/otm/lock/wait/NoWaitStrategy.java
Index: NoWaitStrategy.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/lock/wait/NoWaitStrategy.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NoWaitStrategy.java 1 Feb 2003 18:39:57 -0000 1.1
+++ NoWaitStrategy.java 18 May 2003 15:10:49 -0000 1.2
@@ -7,21 +7,21 @@
/**
*
* <javadoc>
- *
+ *
* @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
- *
+ *
*/
public class NoWaitStrategy implements LockWaitStrategy
{
- /**
- * @see org.apache.ojb.otm.lock.wait.LockWaitStrategy#waitForLock(ObjectLock, Transaction)
- */
- public void waitForLock(ObjectLock lock, Transaction tx)
- throws LockingException
- {
+ /**
+ * @see org.apache.ojb.otm.lock.wait.LockWaitStrategy#waitForLock(ObjectLock, Transaction)
+ */
+ public void waitForLock(ObjectLock lock, Transaction tx)
+ throws LockingException
+ {
throw new ConcurrentModificationException(
"Object [id: " + lock.getTargetIdentity().toString() + "] locked by Transaction " + tx);
- }
+ }
}
1.1 db-ojb/src/java/org/apache/ojb/otm/lock/wait/TimeoutStrategy.java
Index: TimeoutStrategy.java
===================================================================
package org.apache.ojb.otm.lock.wait;
import org.apache.ojb.otm.core.Transaction;
import org.apache.ojb.otm.lock.LockingException;
import org.apache.ojb.otm.lock.ObjectLock;
/**
*
* Just a timeout
*
* @author <a href="mailto:olegnitz@apache.org">Oleg Nitz</a>
*
*/
public class TimeoutStrategy implements LockWaitStrategy
{
private long _timeout;
/**
* @param timeout the number of milliseconds to wait before throwing exception
*/
public TimeoutStrategy(long timeout) {
if (timeout <= 0)
{
throw new IllegalArgumentException("Illegal timeout value: " + timeout);
}
_timeout = timeout;
}
/**
* The default timeout is 30 seconds
*/
public TimeoutStrategy() {
this(30000);
}
/**
* @see org.apache.ojb.otm.lock.wait.LockWaitStrategy#waitForLock(ObjectLock, Transaction)
*/
public void waitForLock(ObjectLock lock, Transaction tx)
throws LockingException
{
long now = System.currentTimeMillis();
long deadline = System.currentTimeMillis() + _timeout;
do
{
try
{
long toSleep = Math.min(deadline - now, 1000);
Thread.sleep(toSleep);
now += toSleep;
} catch (InterruptedException ex) {
now = System.currentTimeMillis();
}
if (lock.getWriter() == null)
{
return;
}
}
while (now < deadline);
throw new ConcurrentModificationException(
"Object [id: " + lock.getTargetIdentity().toString() + "] locked by Transaction " + tx);
}
}