Java Code Examples for org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback#execute()

The following examples show how to use org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractContentStreamListener.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public final void contentStreamClosed() throws ContentIOException
{
    RetryingTransactionCallback<Object> cb = new RetryingTransactionCallback<Object>()
    {
        public Object execute()
        {
            contentStreamClosedImpl();
            return null;
        }
    };
    if (transactionHelper != null)
    {
        // Execute in transaction.
        transactionHelper.doInTransaction(cb, false);
    }
    else
    {
        try
        {
            cb.execute();       
        }
        catch (Throwable e)
        {
            throw new ContentIOException("Failed to executed channel close callbacks", e);
        }
    }

}
 
Example 2
Source File: CacheTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Execute the callback and ensure that the backing cache is left with the expected value */
private void executeAndCheck(
        RetryingTransactionCallback<Object> callback,
        boolean readOnly,
        String key,
        Object expectedValue,
        boolean mustContainKey) throws Throwable
{
    if (expectedValue != null && !mustContainKey)
    {
        throw new IllegalArgumentException("Why have a value when the key should not be there?");
    }
    
    TransactionService transactionService = serviceRegistry.getTransactionService();
    UserTransaction txn = transactionService.getUserTransaction(readOnly);
    try
    {
        txn.begin();
        callback.execute();
        txn.commit();
    }
    finally
    {
        try { txn.rollback(); } catch (Throwable ee) {}
    }
    Object actualValue = TransactionalCache.getSharedCacheValue(backingCache, key, null);
    assertEquals("Backing cache value was not correct", expectedValue, actualValue);
    assertEquals("Backing cache contains(key): ", mustContainKey, backingCache.contains(key));
    
    // Clear the backing cache to ensure that subsequent tests don't run into existing data
    backingCache.clear();
}
 
Example 3
Source File: DbNodeServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void executeAndCheck(NodeRef nodeRef, RetryingTransactionCallback<Object> callback) throws Throwable
{
    UserTransaction txn = txnService.getUserTransaction();
    txn.begin();
    
    NodeRef.Status currentStatus = nodeService.getNodeStatus(nodeRef);
    assertNotNull(currentStatus);
    String currentTxnId = AlfrescoTransactionSupport.getTransactionId();
    assertNotNull(currentTxnId);
    assertNotSame(currentTxnId, currentStatus.getChangeTxnId());
    try
    {
        callback.execute();
        // get the status
        NodeRef.Status newStatus = nodeService.getNodeStatus(nodeRef);
        assertNotNull(newStatus);
        // check
        assertEquals("Change didn't update status", currentTxnId, newStatus.getChangeTxnId());
        
        // Make sure we can pre-load the node i.e. nodes in all state need to be pre-loadable
        // See CLOUD-1807
        Long nodeId = newStatus.getDbId();
        nodeDAO.getParentAssocs(nodeId, null, null, null, new DummyChildAssocRefQueryCallback());
        nodeDAO.cacheNodesById(Collections.singletonList(nodeId));
        
        txn.commit();
    }
    catch (Throwable e)
    {
        try { txn.rollback(); } catch (Throwable ee) {}
        throw e;
    }
}
 
Example 4
Source File: NodeDAOTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testTransaction() throws Throwable
{
    final boolean[] newTxn = new boolean[] {false};
    RetryingTransactionCallback<Long> getTxnIdCallback = new RetryingTransactionCallback<Long>()
    {
        public Long execute() throws Throwable
        {
            return nodeDAO.getCurrentTransactionId(newTxn[0]);
        }
    };
    // No txn
    try
    {
        getTxnIdCallback.execute();
        fail("Should have failed when running outside of a transaction");
    }
    catch (Throwable e)
    {
        // Expected
    }
    // Read-only
    assertNull("No Txn ID should be present in read-only txn", txnHelper.doInTransaction(getTxnIdCallback, true));
    // First success
    Long txnId1 = txnHelper.doInTransaction(getTxnIdCallback);
    assertNull("No Txn ID should be present in untouched txn", txnId1);
    // Second success
    newTxn[0] = true;
    Long txnId2 = txnHelper.doInTransaction(getTxnIdCallback);
    assertNotNull("Txn ID should be present by forcing it", txnId2);
}