Search in sources :

Example 1 with Transaction

use of org.alfresco.repo.domain.node.Transaction in project alfresco-repository by Alfresco.

the class NodeDAOImpl method selectLastTxnBeforeCommitTime.

@Override
protected Transaction selectLastTxnBeforeCommitTime(Long maxCommitTime) {
    Assert.notNull(maxCommitTime, "maxCommitTime");
    TransactionQueryEntity query = new TransactionQueryEntity();
    query.setMaxCommitTime(maxCommitTime);
    List<Transaction> txns = template.selectList(SELECT_TXN_LAST, query, new RowBounds(0, 1));
    if (txns.size() > 0) {
        return txns.get(0);
    } else {
        return null;
    }
}
Also used : Transaction(org.alfresco.repo.domain.node.Transaction) TransactionQueryEntity(org.alfresco.repo.domain.node.TransactionQueryEntity) RowBounds(org.apache.ibatis.session.RowBounds)

Example 2 with Transaction

use of org.alfresco.repo.domain.node.Transaction in project alfresco-repository by Alfresco.

the class TransactionCleanupTest method testPurgeUnusedTransactions.

@Test
public void testPurgeUnusedTransactions() throws Exception {
    // Execute transactions that update a number of nodes. For nodeRef1, all but the last txn will be unused.
    // run the transaction cleaner to clean up any existing unused transactions
    worker.doClean();
    final long start = System.currentTimeMillis();
    final Long minTxnId = nodeDAO.getMinTxnId();
    final Map<NodeRef, List<String>> txnIds = createTransactions();
    final List<String> txnIds1 = txnIds.get(nodeRef1);
    final List<String> txnIds2 = txnIds.get(nodeRef2);
    final List<String> txnIds3 = txnIds.get(nodeRef3);
    // Pure delete: final List<String> txnIds4 = txnIds.get(nodeRef4);
    // Pure delete: final List<String> txnIds5 = txnIds.get(nodeRef5);
    // Double-check that n4 and n5 are present in deleted form
    nodesCache.clear();
    UserTransaction txn = transactionService.getUserTransaction(true);
    txn.begin();
    try {
        assertNotNull("Node 4 is deleted but not purged", nodeDAO.getNodeRefStatus(nodeRef4));
        assertNotNull("Node 5 is deleted but not purged", nodeDAO.getNodeRefStatus(nodeRef5));
    } finally {
        txn.rollback();
    }
    // run the transaction cleaner
    // small purge size
    worker.setPurgeSize(5);
    List<String> reports = worker.doClean();
    for (String report : reports) {
        logger.debug(report);
    }
    // Get transactions committed after the test started
    RetryingTransactionCallback<List<Transaction>> getTxnsCallback = new RetryingTransactionCallback<List<Transaction>>() {

        @Override
        public List<Transaction> execute() throws Throwable {
            return ((NodeDAOImpl) nodeDAO).selectTxns(Long.valueOf(start), Long.valueOf(Long.MAX_VALUE), Integer.MAX_VALUE, null, null, true);
        }
    };
    List<Transaction> txns = transactionService.getRetryingTransactionHelper().doInTransaction(getTxnsCallback, true, false);
    List<String> expectedUnusedTxnIds = new ArrayList<String>(10);
    expectedUnusedTxnIds.addAll(txnIds1.subList(0, txnIds1.size() - 1));
    List<String> expectedUsedTxnIds = new ArrayList<String>(5);
    expectedUsedTxnIds.add(txnIds1.get(txnIds1.size() - 1));
    expectedUsedTxnIds.addAll(txnIds2);
    expectedUsedTxnIds.addAll(txnIds3);
    // 4 and 5 should not be in the list because they are deletes
    // check that the correct transactions have been purged i.e. all except the last one to update the node
    // i.e. in this case, all but the last one in txnIds1
    int numFoundUnusedTxnIds = 0;
    for (String txnId : expectedUnusedTxnIds) {
        if (!containsTransaction(txns, txnId)) {
            numFoundUnusedTxnIds++;
        } else if (txnIds1.contains(txnId)) {
            fail("Unused transaction(s) were not purged: " + txnId);
        }
    }
    assertEquals(9, numFoundUnusedTxnIds);
    // check that the correct transactions remain i.e. all those in txnIds2, txnIds3, txnIds4 and txnIds5
    int numFoundUsedTxnIds = 0;
    for (String txnId : expectedUsedTxnIds) {
        if (containsTransaction(txns, txnId)) {
            numFoundUsedTxnIds++;
        }
    }
    assertEquals(3, numFoundUsedTxnIds);
    // Get transactions committed after the test started
    RetryingTransactionCallback<List<Long>> getTxnsUnusedCallback = new RetryingTransactionCallback<List<Long>>() {

        @Override
        public List<Long> execute() throws Throwable {
            return nodeDAO.getTxnsUnused(minTxnId, Long.MAX_VALUE, Integer.MAX_VALUE);
        }
    };
    List<Long> txnsUnused = transactionService.getRetryingTransactionHelper().doInTransaction(getTxnsUnusedCallback, true, false);
    assertEquals(0, txnsUnused.size());
    // Double-check that n4 and n5 were removed as well
    nodesCache.clear();
    assertNull("Node 4 was not cleaned up", nodeDAO.getNodeRefStatus(nodeRef4));
    assertNull("Node 5 was not cleaned up", nodeDAO.getNodeRefStatus(nodeRef5));
}
Also used : UserTransaction(javax.transaction.UserTransaction) ArrayList(java.util.ArrayList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Transaction(org.alfresco.repo.domain.node.Transaction) UserTransaction(javax.transaction.UserTransaction) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) NodeDAOImpl(org.alfresco.repo.domain.node.ibatis.NodeDAOImpl) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Transaction (org.alfresco.repo.domain.node.Transaction)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UserTransaction (javax.transaction.UserTransaction)1 TransactionQueryEntity (org.alfresco.repo.domain.node.TransactionQueryEntity)1 NodeDAOImpl (org.alfresco.repo.domain.node.ibatis.NodeDAOImpl)1 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 RowBounds (org.apache.ibatis.session.RowBounds)1 Test (org.junit.Test)1