Search in sources :

Example 1 with OptimisticLockManagerProxy

use of org.jboss.stm.internal.proxy.OptimisticLockManagerProxy in project narayana by jbosstm.

the class Container method getContainer.

/**
 * Given the proxy return the container that is managing it.
 *
 * @param proxy the instance within the container we're looking for.
 * @return the container or null. Shouldn't really be possible to get null!
 */
public static final Container<?> getContainer(Object proxy) {
    /*
         * Rather than maintain a list of Container instances and iterate through them
         * we create a clone of the Container using the real container within the
         * proxy itself. Container is essentially a (almost) stateless wrapper class
         * anyway so hopefully this is a lightweight and faster way of achieving this.
         * Can revisit later if this creates too many instances.
         */
    Container<?> toReturn = null;
    if (proxy instanceof OptimisticLockManagerProxy<?>) {
        RecoverableContainer<?> cont = ((OptimisticLockManagerProxy<?>) proxy).getContainer();
        toReturn = new Container(cont);
    } else {
        if (proxy instanceof LockManagerProxy<?>) {
            RecoverableContainer<?> cont = ((LockManagerProxy<?>) proxy).getContainer();
            toReturn = new Container(cont);
        } else
            throw new IllegalArgumentException("Not a proxy object!");
    }
    return toReturn;
}
Also used : RecoverableContainer(org.jboss.stm.internal.RecoverableContainer) PersistentContainer(org.jboss.stm.internal.PersistentContainer) OptimisticLockManagerProxy(org.jboss.stm.internal.proxy.OptimisticLockManagerProxy) LockManagerProxy(org.jboss.stm.internal.proxy.LockManagerProxy) OptimisticLockManagerProxy(org.jboss.stm.internal.proxy.OptimisticLockManagerProxy)

Example 2 with OptimisticLockManagerProxy

use of org.jboss.stm.internal.proxy.OptimisticLockManagerProxy in project narayana by jbosstm.

the class ContainerUnitTest method testOptimisticHammer.

public void testOptimisticHammer() {
    if (true)
        return;
    Container<Sample1> theContainer = new Container<Sample1>();
    Sample1 obj1 = theContainer.create(new Sample1Imple(10));
    Sample1 obj2 = theContainer.create(new Sample1Imple(10));
    Sample1 obj3 = theContainer.clone(new Sample1Imple(), obj1);
    Sample1 obj4 = theContainer.clone(new Sample1Imple(), obj2);
    int workers = 2;
    Worker[] worker = new Worker[workers];
    assertTrue(obj3 != null);
    assertTrue(obj4 != null);
    /*
         * TODO cannot share state until the state is written, and it isn't written
         * until an explicit set is called. What we want is for the state to be in the
         * store when create (clone) returns.
         * 
         * So currently you need to force the saving of the object states (we use increment
         * here for that purpose) and then force a read of the state through the clones
         * (we use value for that purpose). Not as opaque as we'd like and we should be able
         * to force the save and restore via the proxy classes.
         */
    AtomicAction A = new AtomicAction();
    A.begin();
    obj1.increment();
    obj2.increment();
    A.commit();
    assertEquals(obj1.value(), 11);
    assertEquals(obj2.value(), 11);
    A = new AtomicAction();
    A.begin();
    assertEquals(obj3.value(), 11);
    A.commit();
    worker[0] = new Worker(obj1, obj2);
    worker[1] = new Worker(obj3, obj4);
    for (int j = 0; j < workers; j++) worker[j].start();
    try {
        for (int k = 0; k < workers; k++) worker[k].join();
    } catch (final Throwable ex) {
    }
    assertEquals(obj1.value() + obj2.value(), 22);
    @SuppressWarnings("unchecked") RecoverableContainer<Sample1> cont = ((OptimisticLockManagerProxy<Sample1>) obj1).getContainer();
    assertTrue(cont != null);
    @SuppressWarnings("unchecked") Container<Sample1> duplicateContainer = (Container<Sample1>) Container.getContainer(obj1);
    assertTrue(duplicateContainer != null);
}
Also used : AtomicAction(com.arjuna.ats.arjuna.AtomicAction) RecoverableContainer(org.jboss.stm.internal.RecoverableContainer) OptimisticLockManagerProxy(org.jboss.stm.internal.proxy.OptimisticLockManagerProxy)

Aggregations

RecoverableContainer (org.jboss.stm.internal.RecoverableContainer)2 OptimisticLockManagerProxy (org.jboss.stm.internal.proxy.OptimisticLockManagerProxy)2 AtomicAction (com.arjuna.ats.arjuna.AtomicAction)1 PersistentContainer (org.jboss.stm.internal.PersistentContainer)1 LockManagerProxy (org.jboss.stm.internal.proxy.LockManagerProxy)1