use of org.datanucleus.samples.concurrency.Transfer in project tests by datanucleus.
the class ConcurrencyTest method testBasicConcurrency.
public void testBasicConcurrency() {
// Persist Accounts and Transfers
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(new Account("alice", 1000));
pm.makePersistent(new Account("berta", 0));
pm.makePersistent(new Account("charly", 0));
pm.makePersistent(new Transfer("alice", "berta", 100));
pm.makePersistent(new Transfer("alice", "charly", 200));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Process the Transfers, one per Thread, and one PM per Thread
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
tx = pm.currentTransaction();
try {
tx.begin();
LinkedList<Thread> threads = new LinkedList<>();
Extent ext = pm.getExtent(Transfer.class, true);
Iterator iter = ext.iterator();
while (iter.hasNext()) {
Transfer t = (Transfer) iter.next();
threads.add(startConcurrentTransfer(JDOHelper.getObjectId(t)));
}
tx.commit();
while (!threads.isEmpty()) {
Thread td = (Thread) threads.removeFirst();
try {
td.join();
} catch (InterruptedException e) {
}
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the results
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Transfer.class, true);
Iterator iter = ext.iterator();
while (iter.hasNext()) {
Transfer transfer = (Transfer) iter.next();
assertTrue(transfer.isBooked());
}
ext = pm.getExtent(Account.class, true);
iter = ext.iterator();
while (iter.hasNext()) {
Account acct = (Account) iter.next();
String name = acct.getName();
if ("alice".equals(name)) {
assertEquals(700, acct.getSaldo());
} else if ("berta".equals(name)) {
assertEquals(100, acct.getSaldo());
} else if ("charly".equals(name)) {
assertEquals(200, acct.getSaldo());
} else {
assertFalse("unexpected account name: " + name, true);
}
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.samples.concurrency.Transfer in project tests by datanucleus.
the class ConcurrencyTest method startConcurrentTransfer.
private Thread startConcurrentTransfer(final Object transferId) {
// Perform the transfer in its own PM
final PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
final Object lock = new Object();
Thread td = new Thread() {
public void run() {
LOG.info(">> Starting thread " + transferId + " in pm=" + pm);
synchronized (lock) {
lock.notifyAll();
}
pm.currentTransaction().begin();
Transfer t = (Transfer) pm.getObjectById(transferId, true);
performTransfer(t);
LOG.info(">> Completed thread " + transferId + " in pm=" + pm);
}
};
synchronized (lock) {
td.start();
try {
lock.wait();
} catch (InterruptedException e) {
}
}
return td;
}
Aggregations