Search in sources :

Example 1 with Transfer

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();
    }
}
Also used : Account(org.datanucleus.samples.concurrency.Account) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Transfer(org.datanucleus.samples.concurrency.Transfer) Iterator(java.util.Iterator) LinkedList(java.util.LinkedList)

Example 2 with Transfer

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;
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Transfer(org.datanucleus.samples.concurrency.Transfer)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 Transfer (org.datanucleus.samples.concurrency.Transfer)2 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Extent (javax.jdo.Extent)1 Transaction (javax.jdo.Transaction)1 Account (org.datanucleus.samples.concurrency.Account)1