Example usage for javax.transaction Status STATUS_ACTIVE

List of usage examples for javax.transaction Status STATUS_ACTIVE

Introduction

In this page you can find the example usage for javax.transaction Status STATUS_ACTIVE.

Prototype

int STATUS_ACTIVE

To view the source code for javax.transaction Status STATUS_ACTIVE.

Click Source Link

Document

A transaction is associated with the target object and it is in the active state.

Usage

From source file:org.alfresco.repo.cache.CacheTest.java

public void testTransactionalCacheDisableSharedCaches() throws Throwable {
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_ONE, NEW_GLOBAL_ONE, null);
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_TWO, NEW_GLOBAL_TWO, null);
    TransactionalCache.putSharedCacheValue(backingCache, NEW_GLOBAL_THREE, NEW_GLOBAL_THREE, null);

    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();
    try {/*w ww.j a  v a  2  s . c om*/
        // begin a transaction
        txn.begin();

        // Go directly past ALL shared caches
        transactionalCache.setDisableSharedCacheReadForTransaction(true);

        // Try to get results in shared caches
        assertNull("Read of mutable shared cache MUST NOT use backing cache",
                transactionalCache.get(NEW_GLOBAL_ONE));
        assertNull("Value should not be in any cache", transactionalCache.get(UPDATE_TXN_THREE));

        // Update the transactional caches
        transactionalCache.put(NEW_GLOBAL_TWO, "An update");
        transactionalCache.put(UPDATE_TXN_THREE, UPDATE_TXN_THREE);

        // Try to get results in shared caches
        assertNull("Read of mutable shared cache MUST NOT use backing cache",
                transactionalCache.get(NEW_GLOBAL_ONE));
        assertEquals("Value should be in transactional cache", "An update",
                transactionalCache.get(NEW_GLOBAL_TWO));
        assertEquals("Value should be in transactional cache", UPDATE_TXN_THREE,
                transactionalCache.get(UPDATE_TXN_THREE));

        txn.commit();

        // Now check that values were not written through for any caches
        assertEquals("Out-of-txn read must return shared value", NEW_GLOBAL_ONE,
                transactionalCache.get(NEW_GLOBAL_ONE));
        assertNull("Value should be removed from shared cache", transactionalCache.get(NEW_GLOBAL_TWO));
        assertEquals("New values must be written to shared cache", UPDATE_TXN_THREE,
                transactionalCache.get(UPDATE_TXN_THREE));
    } catch (Throwable e) {
        if (txn.getStatus() == Status.STATUS_ACTIVE) {
            txn.rollback();
        }
        throw e;
    }
}

From source file:org.alfresco.repo.cache.CacheTest.java

public void testTransactionalCacheStatsOnCommit() throws Throwable {
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test1", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test2", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test3", "v", null);

    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();

    final long hitsAtStart = cacheStats.count("transactionalCache", OpType.GET_HIT);
    final long missesAtStart = cacheStats.count("transactionalCache", OpType.GET_MISS);
    final long putsAtStart = cacheStats.count("transactionalCache", OpType.PUT);
    final long removesAtStart = cacheStats.count("transactionalCache", OpType.REMOVE);
    final long clearsAtStart = cacheStats.count("transactionalCache", OpType.CLEAR);

    try {//from  w  w w . j ava  2  s .  c  o m
        // begin a transaction
        txn.begin();

        // Perform some puts
        transactionalCache.put("stats-test4", "v");
        transactionalCache.put("stats-test5", "v");
        transactionalCache.put("stats-test6", "v");
        transactionalCache.put("stats-test7", "v");
        transactionalCache.put("stats-test8", "v");

        // Perform some gets...
        // hits
        transactionalCache.get("stats-test3");
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // repeated hits won't touch the shared cache
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // misses - not yet committed
        transactionalCache.get("stats-miss1");
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");
        transactionalCache.get("stats-miss4");
        // repeated misses won't touch the shared cache
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");

        // Perform some removals
        transactionalCache.remove("stats-test1");
        transactionalCache.remove("stats-test2");
        transactionalCache.remove("stats-test3");
        transactionalCache.remove("stats-test9");
        transactionalCache.remove("stats-test10");
        transactionalCache.remove("stats-test11");
        transactionalCache.remove("stats-test12");
        transactionalCache.remove("stats-test13");

        // Check nothing has changed yet - changes not written through until commit or rollback
        assertEquals(hitsAtStart, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart, cacheStats.count("transactionalCache", OpType.GET_MISS));
        assertEquals(putsAtStart, cacheStats.count("transactionalCache", OpType.PUT));
        assertEquals(removesAtStart, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(clearsAtStart, cacheStats.count("transactionalCache", OpType.CLEAR));

        // commit the transaction
        txn.commit();

        // TODO: remove is called twice for each remove (in beforeCommit and afterCommit) - check this is correct.
        assertEquals(removesAtStart + 16, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(hitsAtStart + 3, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart + 4, cacheStats.count("transactionalCache", OpType.GET_MISS));
        assertEquals(putsAtStart + 5, cacheStats.count("transactionalCache", OpType.PUT));
        // Performing a clear would affect the other stats, so a separate test is required.
        assertEquals(clearsAtStart + 0, cacheStats.count("transactionalCache", OpType.CLEAR));
    } catch (Throwable e) {
        if (txn.getStatus() == Status.STATUS_ACTIVE) {
            txn.rollback();
        }
        throw e;
    }
}

From source file:org.alfresco.repo.cache.CacheTest.java

public void testTransactionalCacheStatsDisabled() throws Throwable {
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCacheNoStats, "stats-test1", "v", null);
    TransactionalCache.putSharedCacheValue(backingCacheNoStats, "stats-test2", "v", null);
    TransactionalCache.putSharedCacheValue(backingCacheNoStats, "stats-test3", "v", null);

    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();

    for (OpType opType : OpType.values()) {
        try {//from   ww w. jav  a2s .co m
            cacheStats.count("transactionalCacheNoStats", opType);
            fail("Expected NoStatsForCache error.");
        } catch (NoStatsForCache e) {
            // Good
        }
    }

    try {
        // begin a transaction
        txn.begin();

        // Perform some puts
        transactionalCacheNoStats.put("stats-test4", "v");
        transactionalCache.put("stats-test5", "v");
        transactionalCache.put("stats-test6", "v");
        transactionalCache.put("stats-test7", "v");
        transactionalCache.put("stats-test8", "v");

        // Perform some gets...
        // hits
        transactionalCache.get("stats-test3");
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // repeated hits won't touch the shared cache
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // misses - not yet committed
        transactionalCache.get("stats-miss1");
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");
        transactionalCache.get("stats-miss4");
        // repeated misses won't touch the shared cache
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");

        // Perform some removals
        transactionalCache.remove("stats-test1");
        transactionalCache.remove("stats-test2");
        transactionalCache.remove("stats-test3");
        transactionalCache.remove("stats-test9");
        transactionalCache.remove("stats-test10");
        transactionalCache.remove("stats-test11");
        transactionalCache.remove("stats-test12");
        transactionalCache.remove("stats-test13");

        // Check nothing has changed - changes not written through until commit or rollback
        for (OpType opType : OpType.values()) {
            try {
                cacheStats.count("transactionalCacheNoStats", opType);
                fail("Expected NoStatsForCache error.");
            } catch (NoStatsForCache e) {
                // Good
            }
        }

        // commit the transaction
        txn.commit();

        // Post-commit, nothing should have changed.
        for (OpType opType : OpType.values()) {
            try {
                cacheStats.count("transactionalCacheNoStats", opType);
                fail("Expected NoStatsForCache error.");
            } catch (NoStatsForCache e) {
                // Good
            }
        }
    } catch (Throwable e) {
        if (txn.getStatus() == Status.STATUS_ACTIVE) {
            txn.rollback();
        }
        throw e;
    }
}

From source file:org.alfresco.repo.cache.CacheTest.java

public void testTransactionalCacheStatsForClears() throws Throwable {
    // add item to global cache
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test1", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test2", "v", null);
    TransactionalCache.putSharedCacheValue(backingCache, "stats-test3", "v", null);

    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction();

    final long hitsAtStart = cacheStats.count("transactionalCache", OpType.GET_HIT);
    final long missesAtStart = cacheStats.count("transactionalCache", OpType.GET_MISS);
    final long putsAtStart = cacheStats.count("transactionalCache", OpType.PUT);
    final long removesAtStart = cacheStats.count("transactionalCache", OpType.REMOVE);
    final long clearsAtStart = cacheStats.count("transactionalCache", OpType.CLEAR);

    try {//from   w w w  . j  av a 2  s .  c  om
        // begin a transaction
        txn.begin();

        // Perform some puts
        transactionalCache.put("stats-test4", "v");
        transactionalCache.put("stats-test5", "v");
        transactionalCache.put("stats-test6", "v");
        transactionalCache.put("stats-test7", "v");
        transactionalCache.put("stats-test8", "v");

        // Perform some gets...
        // hits
        transactionalCache.get("stats-test3");
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // repeated hits won't touch the shared cache
        transactionalCache.get("stats-test2");
        transactionalCache.get("stats-test1");
        // misses - not yet committed
        transactionalCache.get("stats-miss1");
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");
        transactionalCache.get("stats-miss4");
        // repeated misses won't touch the shared cache
        transactionalCache.get("stats-miss2");
        transactionalCache.get("stats-miss3");

        // Perform some removals
        transactionalCache.remove("stats-test1");
        transactionalCache.remove("stats-test2");
        transactionalCache.remove("stats-test3");
        transactionalCache.remove("stats-test9");
        transactionalCache.remove("stats-test10");
        transactionalCache.remove("stats-test11");
        transactionalCache.remove("stats-test12");
        transactionalCache.remove("stats-test13");

        // Perform some clears
        transactionalCache.clear();
        transactionalCache.clear();

        // Check nothing has changed yet - changes not written through until commit or rollback
        assertEquals(hitsAtStart, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart, cacheStats.count("transactionalCache", OpType.GET_MISS));
        assertEquals(putsAtStart, cacheStats.count("transactionalCache", OpType.PUT));
        assertEquals(removesAtStart, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(clearsAtStart, cacheStats.count("transactionalCache", OpType.CLEAR));

        // commit the transaction
        txn.commit();

        assertEquals(clearsAtStart + 2, cacheStats.count("transactionalCache", OpType.CLEAR));
        // There are no removes or puts propagated to the shared cache, as a result of the clears.
        assertEquals(removesAtStart + 0, cacheStats.count("transactionalCache", OpType.REMOVE));
        assertEquals(putsAtStart + 0, cacheStats.count("transactionalCache", OpType.PUT));
        assertEquals(hitsAtStart + 3, cacheStats.count("transactionalCache", OpType.GET_HIT));
        assertEquals(missesAtStart + 4, cacheStats.count("transactionalCache", OpType.GET_MISS));
    } catch (Throwable e) {
        if (txn.getStatus() == Status.STATUS_ACTIVE) {
            txn.rollback();
        }
        throw e;
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl.java

/**
 * Commit this index// w  w  w  .j a v  a  2s  .c  om
 * 
 * @throws LuceneIndexException
 */
public void commit() throws LuceneIndexException {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug(Thread.currentThread().getName() + " Starting Commit");
    }
    switch (getStatus().getStatus()) {
    case Status.STATUS_COMMITTING:
        throw new LuceneIndexException("Unable to commit: Transaction is committing");
    case Status.STATUS_COMMITTED:
        throw new LuceneIndexException("Unable to commit: Transaction is commited ");
    case Status.STATUS_ROLLING_BACK:
        throw new LuceneIndexException("Unable to commit: Transaction is rolling back");
    case Status.STATUS_ROLLEDBACK:
        throw new LuceneIndexException("Unable to commit: Transaction is aleady rolled back");
    case Status.STATUS_MARKED_ROLLBACK:
        throw new LuceneIndexException("Unable to commit: Transaction is marked for roll back");
    case Status.STATUS_PREPARING:
        throw new LuceneIndexException("Unable to commit: Transaction is preparing");
    case Status.STATUS_ACTIVE:
        // special case - commit from active
        prepare();
        // drop through to do the commit;
    default:
        if (getStatus().getStatus() != Status.STATUS_PREPARED) {
            throw new LuceneIndexException("Index must be prepared to commit");
        }
        try {
            setStatus(TransactionStatus.COMMITTING);
            if (isModified()) {
                doCommit();
            }
            setStatus(TransactionStatus.COMMITTED);
        } catch (LuceneIndexException e) {
            // If anything goes wrong we try and do a roll back
            rollback();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Commit Failed", e);
            }
            throw new LuceneIndexException("Commit failed", e);
        } catch (Throwable t) {
            // If anything goes wrong we try and do a roll back
            rollback();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Commit Failed", t);
            }
            throw new LuceneIndexException("Commit failed", t);
        } finally {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Ending Commit");
            }

            // Make sure we tidy up
            // deleteDelta();
        }
        break;
    }
}

From source file:org.alfresco.repo.search.impl.lucene.ADMLuceneTest.java

@Override
protected void tearDown() throws Exception {

    if (testTX.getStatus() == Status.STATUS_ACTIVE) {
        testTX.rollback();/*  w  w  w.j  a v a  2 s .c o m*/
    }
    AuthenticationUtil.clearCurrentSecurityContext();
    super.tearDown();
}

From source file:org.alfresco.repo.transaction.RetryingTransactionHelperTest.java

/**
 * Check that the transaction state can be fetched in and around the transaction.
 * This also checks that any mischievous attempts to manipulate the transaction
 * (other than setRollback) are detected. 
 *///  ww  w.  j  a  va2s  .  c  o  m
public void testUserTransactionStatus() {
    UserTransaction txnBefore = RetryingTransactionHelper.getActiveUserTransaction();
    assertNull("Did not expect to get an active UserTransaction", txnBefore);

    RetryingTransactionCallback<Long> callbackOuter = new RetryingTransactionCallback<Long>() {
        public Long execute() throws Throwable {
            final UserTransaction txnOuter = RetryingTransactionHelper.getActiveUserTransaction();
            assertNotNull("Expected an active UserTransaction", txnOuter);
            assertEquals("Should be read-write txn", TxnReadState.TXN_READ_WRITE,
                    AlfrescoTransactionSupport.getTransactionReadState());
            assertEquals("Expected state is active", Status.STATUS_ACTIVE, txnOuter.getStatus());
            RetryingTransactionCallback<Long> callbackInner = new RetryingTransactionCallback<Long>() {
                public Long execute() throws Throwable {
                    UserTransaction txnInner = RetryingTransactionHelper.getActiveUserTransaction();
                    assertNotNull("Expected an active UserTransaction", txnInner);
                    assertEquals("Should be read-only txn", TxnReadState.TXN_READ_ONLY,
                            AlfrescoTransactionSupport.getTransactionReadState());
                    assertEquals("Expected state is active", Status.STATUS_ACTIVE, txnInner.getStatus());
                    // Check blow up
                    try {
                        txnInner.commit();
                        fail("Should not be able to commit the UserTransaction.  It is for info only.");
                    } catch (Throwable e) {
                        // Expected
                    }
                    // Force a rollback
                    txnInner.setRollbackOnly();
                    // Done
                    return null;
                }
            };
            return txnHelper.doInTransaction(callbackInner, true, true);
        }
    };
    txnHelper.doInTransaction(callbackOuter);

    UserTransaction txnAfter = RetryingTransactionHelper.getActiveUserTransaction();
    assertNull("Did not expect to get an active UserTransaction", txnAfter);
}

From source file:org.apache.ode.bpel.engine.Contexts.java

public boolean isTransacted() {
    try {/* w  ww. j a  v  a 2 s  .  c  o m*/
        return txManager.getStatus() == Status.STATUS_ACTIVE;
    } catch (SystemException e) {
        throw new BpelEngineException(e);
    }
}

From source file:org.apache.ode.daohib.JotmTransaction.java

/**
 * {@inheritDoc}/* w  w w .  j a v  a 2s . c  o  m*/
 */
public boolean isActive() throws TransactionException {
    if (!begun || commitFailed || commitSucceeded) {
        return false;
    }

    final int status;
    try {
        status = userTransaction.getStatus();
    } catch (SystemException se) {
        log.error("Could not determine transaction status", se);
        throw new TransactionException("Could not determine transaction status: ", se);
    }
    if (status == Status.STATUS_UNKNOWN) {
        throw new TransactionException("Could not determine transaction status");
    } else {
        return status == Status.STATUS_ACTIVE;
    }
}

From source file:org.apache.ofbiz.entity.transaction.TransactionUtil.java

/** Begins a transaction in the current thread IF transactions are available; only
 * tries if the current transaction status is ACTIVE, if not active it returns false.
 * If and on only if it begins a transaction it will return true. In other words, if
 * a transaction is already in place it will return false and do nothing.
 */// w w  w. ja  v a 2 s .  c  o m
public static boolean begin(int timeout) throws GenericTransactionException {
    UserTransaction ut = TransactionFactoryLoader.getInstance().getUserTransaction();
    if (ut != null) {
        try {
            int currentStatus = ut.getStatus();
            if (Debug.verboseOn()) {
                Debug.logVerbose("Current status : " + getTransactionStateString(currentStatus), module);
            }
            if (currentStatus == Status.STATUS_ACTIVE) {
                if (Debug.verboseOn()) {
                    Debug.logVerbose("Active transaction in place, so no transaction begun", module);
                }
                return false;
            } else if (currentStatus == Status.STATUS_MARKED_ROLLBACK) {
                Exception e = getTransactionBeginStack();
                if (e != null) {
                    Debug.logWarning(e,
                            "Active transaction marked for rollback in place, so no transaction begun; this stack trace shows when the exception began: ",
                            module);
                } else {
                    Debug.logWarning("Active transaction marked for rollback in place, so no transaction begun",
                            module);
                }

                RollbackOnlyCause roc = getSetRollbackOnlyCause();
                // do we have a cause? if so, throw special exception
                if (UtilValidate.isNotEmpty(roc)) {
                    throw new GenericTransactionException(
                            "The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: "
                                    + roc.getCauseMessage(),
                            roc.getCauseThrowable());
                } else {
                    return false;
                }
            }

            internalBegin(ut, timeout);

            // reset the transaction stamps, just in case...
            clearTransactionStamps();
            // initialize the start stamp
            getTransactionStartStamp();
            // set the tx begin stack placeholder
            setTransactionBeginStack();

            // initialize the debug resource
            if (debugResources()) {
                DebugXaResource dxa = new DebugXaResource();
                try {
                    dxa.enlist();
                } catch (XAException e) {
                    Debug.logError(e, module);
                }
            }

            return true;
        } catch (NotSupportedException e) {
            throw new GenericTransactionException(
                    "Not Supported error, could not begin transaction (probably a nesting problem)", e);
        } catch (SystemException e) {
            throw new GenericTransactionException("System error, could not begin transaction", e);
        }
    } else {
        if (Debug.infoOn())
            Debug.logInfo("No user transaction, so no transaction begun", module);
        return false;
    }
}