Search in sources :

Example 1 with UserTransactionImpl

use of org.apache.jackrabbit.core.UserTransactionImpl in project jackrabbit by apache.

the class XATransaction method execute.

public NodeIterator execute() throws Exception {
    UserTransactionImpl tx = new UserTransactionImpl(getSession());
    log.info("begin transaction");
    tx.begin();
    NodeIterator it = op.execute();
    log.info("commit transaction");
    tx.commit();
    return it;
}
Also used : NodeIterator(javax.jcr.NodeIterator) UserTransactionImpl(org.apache.jackrabbit.core.UserTransactionImpl)

Example 2 with UserTransactionImpl

use of org.apache.jackrabbit.core.UserTransactionImpl in project jackrabbit by apache.

the class ConcurrentLockingWithTransactionsTest method testConcurrentCreateAndLockUnLockInTransaction.

public void testConcurrentCreateAndLockUnLockInTransaction() throws RepositoryException {
    runTask(new Task() {

        public void execute(Session session, Node test) throws RepositoryException {
            // add versionable nodes
            for (int i = 0; i < NUM_OPERATIONS / CONCURRENCY; i++) {
                try {
                    final UserTransaction utx = new UserTransactionImpl(test.getSession());
                    utx.begin();
                    Node n = test.addNode("test" + i);
                    n.addMixin(mixLockable);
                    session.save();
                    Lock l = n.lock(false, true);
                    n.unlock();
                    utx.commit();
                } catch (Exception e) {
                    final String threadName = Thread.currentThread().getName();
                    final Throwable deepCause = getLevel2Cause(e);
                    if (deepCause != null && deepCause instanceof StaleItemStateException) {
                    // ignore
                    } else {
                        throw new RepositoryException(threadName + ", i=" + i + ":" + e.getClass().getName(), e);
                    }
                }
            }
        }
    }, CONCURRENCY);
}
Also used : UserTransaction(javax.transaction.UserTransaction) StaleItemStateException(org.apache.jackrabbit.core.state.StaleItemStateException) Node(javax.jcr.Node) UserTransactionImpl(org.apache.jackrabbit.core.UserTransactionImpl) RepositoryException(javax.jcr.RepositoryException) RepositoryException(javax.jcr.RepositoryException) StaleItemStateException(org.apache.jackrabbit.core.state.StaleItemStateException) Session(javax.jcr.Session) Lock(javax.jcr.lock.Lock)

Example 3 with UserTransactionImpl

use of org.apache.jackrabbit.core.UserTransactionImpl in project jackrabbit by apache.

the class LockTimeoutTest method testExpired.

private void testExpired(boolean xa) throws Exception {
    Session s = testRootNode.getSession();
    Node n = testRootNode.addNode(nodeName1);
    n.addMixin(mixLockable);
    s.save();
    UserTransaction utx = null;
    if (xa) {
        utx = new UserTransactionImpl(s);
        utx.begin();
    }
    javax.jcr.lock.LockManager lm = s.getWorkspace().getLockManager();
    boolean isDeep;
    boolean isSessionScoped;
    long timeoutHint;
    String ownerInfo;
    isDeep = false;
    isSessionScoped = false;
    timeoutHint = 1;
    ownerInfo = "";
    Session s2 = getHelper().getSuperuserSession();
    Lock l = lm.lock(n.getPath(), isDeep, isSessionScoped, timeoutHint, ownerInfo);
    // this works only for timeout = 1,
    // as getSecondsRemaining always returns a positive value
    assertEquals(timeoutHint, l.getSecondsRemaining());
    assertTrue(l.isLive());
    if (xa) {
        utx.commit();
    }
    long start = System.currentTimeMillis();
    while (true) {
        Thread.sleep(100);
        long now = System.currentTimeMillis();
        boolean success;
        try {
            s2.getNode(n.getPath()).setProperty("x", 1);
            s2.save();
            success = true;
        } catch (Exception e) {
            success = false;
        }
        long t = now - start;
        if (t > timeoutHint + 3000) {
            assertTrue(success);
            break;
        } else if (t < timeoutHint) {
            assertFalse(success);
        }
    }
    n.remove();
    s.save();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Node(javax.jcr.Node) UserTransactionImpl(org.apache.jackrabbit.core.UserTransactionImpl) Session(javax.jcr.Session) Lock(javax.jcr.lock.Lock)

Example 4 with UserTransactionImpl

use of org.apache.jackrabbit.core.UserTransactionImpl in project jackrabbit by apache.

the class VersionEventsTest method testXACheckin.

/**
 * Test if checkin of a node in an XA transaction creates two add node
 * events: one for the version and one for the frozen node.
 */
public void testXACheckin() throws Exception {
    // create versionable node
    Node n1 = testRootNode.addNode(nodeName1);
    n1.addMixin(mixVersionable);
    testRootNode.save();
    EventResult listener = new EventResult(log);
    addEventListener(listener, Event.NODE_ADDED);
    // use a transaction
    UserTransaction utx = new UserTransactionImpl(superuser);
    // start transaction
    utx.begin();
    Version v = n1.checkin();
    // commit transaction
    utx.commit();
    removeEventListener(listener);
    Event[] events = listener.getEvents(1000);
    Set paths = new HashSet();
    for (int i = 0; i < events.length; i++) {
        paths.add(events[i].getPath());
    }
    assertTrue("missing 'node added' event: " + v.getPath(), paths.contains(v.getPath()));
    String frozenPath = v.getPath() + "/" + jcrFrozenNode;
    assertTrue("missing 'node added' event: " + frozenPath, paths.contains(frozenPath));
}
Also used : UserTransaction(javax.transaction.UserTransaction) EventResult(org.apache.jackrabbit.test.api.observation.EventResult) HashSet(java.util.HashSet) Set(java.util.Set) Version(javax.jcr.version.Version) Node(javax.jcr.Node) UserTransactionImpl(org.apache.jackrabbit.core.UserTransactionImpl) Event(javax.jcr.observation.Event) HashSet(java.util.HashSet)

Example 5 with UserTransactionImpl

use of org.apache.jackrabbit.core.UserTransactionImpl in project jackrabbit by apache.

the class RemoveVersionTest method testRemoveVersionAndCheckinXA.

/**
 * Removes a version in 1 transaction and tries to commit afterwards the
 * versionable node using a 2nd transaction.
 *
 * Tests error reported in JCR-2613
 *
 * @throws Exception if an error occurs
 */
public void testRemoveVersionAndCheckinXA() throws Exception {
    UserTransaction tx = new UserTransactionImpl(superuser);
    tx.begin();
    Node n = testRootNode.addNode(nodeName1);
    n.addMixin(mixVersionable);
    n.addMixin(mixReferenceable);
    testRootNode.save();
    String uuid = n.getUUID();
    // create two versions
    String v1 = n.checkin().getName();
    n.checkout();
    n.checkin();
    n.checkout();
    tx.commit();
    tx = new UserTransactionImpl(superuser);
    tx.begin();
    // remove one version
    n = superuser.getNodeByUUID(uuid);
    n.getVersionHistory().removeVersion(v1);
    n.save();
    tx.commit();
    // new session
    Session session = getHelper().getSuperuserSession();
    tx = new UserTransactionImpl(session);
    tx.begin();
    n = session.getNodeByUUID(uuid);
    n.checkin();
    tx.commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) Node(javax.jcr.Node) UserTransactionImpl(org.apache.jackrabbit.core.UserTransactionImpl) Session(javax.jcr.Session)

Aggregations

UserTransactionImpl (org.apache.jackrabbit.core.UserTransactionImpl)12 Node (javax.jcr.Node)11 UserTransaction (javax.transaction.UserTransaction)9 Session (javax.jcr.Session)6 Version (javax.jcr.version.Version)4 Lock (javax.jcr.lock.Lock)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 RepositoryException (javax.jcr.RepositoryException)2 Event (javax.jcr.observation.Event)2 StaleItemStateException (org.apache.jackrabbit.core.state.StaleItemStateException)2 EventResult (org.apache.jackrabbit.test.api.observation.EventResult)2 NodeIterator (javax.jcr.NodeIterator)1 ReferentialIntegrityException (javax.jcr.ReferentialIntegrityException)1 Privilege (javax.jcr.security.Privilege)1 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)1