| 1 | |
package org.apache.ojb.otm.lock.isolation; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import java.util.Collection; |
| 19 | |
|
| 20 | |
import org.apache.ojb.otm.core.Transaction; |
| 21 | |
import org.apache.ojb.otm.lock.LockingException; |
| 22 | |
import org.apache.ojb.otm.lock.ObjectLock; |
| 23 | |
|
| 24 | |
public class SerializableIsolation extends AbstractIsolation |
| 25 | |
{ |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
public void readLock (Transaction tx, ObjectLock lock) |
| 31 | |
throws LockingException |
| 32 | |
{ |
| 33 | |
Collection readers = lock.getReaders(); |
| 34 | |
if (readers.isEmpty()) |
| 35 | |
{ |
| 36 | |
lock.readLock(tx); |
| 37 | |
readers = lock.getReaders(); |
| 38 | |
if (readers.size() > 1) |
| 39 | |
{ |
| 40 | |
lock.releaseLock(tx); |
| 41 | |
readLock(tx, lock); |
| 42 | |
} |
| 43 | |
} |
| 44 | |
else |
| 45 | |
{ |
| 46 | |
Transaction reader = (Transaction) readers.iterator().next(); |
| 47 | |
|
| 48 | |
if (reader != tx) { |
| 49 | |
lock.waitForTx(reader); |
| 50 | |
} |
| 51 | |
} |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public void writeLock (Transaction tx, ObjectLock lock) |
| 58 | |
throws LockingException |
| 59 | |
{ |
| 60 | |
Collection readers = lock.getReaders(); |
| 61 | |
if (readers.isEmpty()) |
| 62 | |
{ |
| 63 | |
readLock(tx, lock); |
| 64 | |
writeLock(tx, lock); |
| 65 | |
} |
| 66 | |
else |
| 67 | |
{ |
| 68 | |
Transaction reader = (Transaction) readers.iterator().next(); |
| 69 | |
if (reader == tx) |
| 70 | |
{ |
| 71 | |
lock.writeLock(tx); |
| 72 | |
} |
| 73 | |
else |
| 74 | |
{ |
| 75 | |
lock.waitForTx(reader); |
| 76 | |
writeLock(tx, lock); |
| 77 | |
} |
| 78 | |
} |
| 79 | |
} |
| 80 | |
|
| 81 | |
} |