Renamed TransactionComponent to TransactionServiceImpl

Fixed naming convention to be 'transactionService', 'TransactionService', but kept an alias 'transactionComponent'.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5905 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2007-06-11 10:10:38 +00:00
parent 23cf6181e6
commit cc948d8c39
21 changed files with 149 additions and 48 deletions

View File

@@ -38,7 +38,7 @@ import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.TransactionComponent;
import org.alfresco.repo.transaction.TransactionServiceImpl;
import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
@@ -72,7 +72,7 @@ public abstract class AbstractReindexComponent implements IndexRecovery
private AuthenticationComponent authenticationComponent;
/** provides transactions to atomically index each missed transaction */
protected TransactionComponent transactionService;
protected TransactionServiceImpl transactionService;
/** the component to index the node hierarchy */
protected Indexer indexer;
/** the FTS indexer that we will prompt to pick up on any un-indexed text */
@@ -136,9 +136,9 @@ public abstract class AbstractReindexComponent implements IndexRecovery
*
* @param transactionComponent provide transactions to index each missed transaction
*/
public void setTransactionComponent(TransactionComponent transactionComponent)
public void setTransactionService(TransactionServiceImpl transactionService)
{
this.transactionService = transactionComponent;
this.transactionService = transactionService;
}
/**

View File

@@ -32,7 +32,7 @@ import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.search.Indexer;
import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.TransactionComponent;
import org.alfresco.repo.transaction.TransactionServiceImpl;
import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
import org.alfresco.service.ServiceRegistry;
@@ -89,7 +89,7 @@ public class IndexRemoteTransactionTrackerTest extends TestCase
indexTracker.setNodeDaoService(nodeDaoService);
indexTracker.setNodeService(nodeService);
indexTracker.setSearcher(searchService);
indexTracker.setTransactionComponent((TransactionComponent)transactionService);
indexTracker.setTransactionService((TransactionServiceImpl)transactionService);
// authenticate
authenticationComponent.setSystemUserAsCurrentUser();

View File

@@ -35,7 +35,7 @@ import org.alfresco.repo.search.Indexer;
import org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl;
import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.TransactionComponent;
import org.alfresco.repo.transaction.TransactionServiceImpl;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.repository.ContentData;
@@ -91,7 +91,7 @@ public class MissingContentReindexComponentTest extends TestCase
reindexer.setNodeDaoService(nodeDaoService);
reindexer.setNodeService(nodeService);
reindexer.setSearcher(searchService);
reindexer.setTransactionComponent((TransactionComponent)transactionService);
reindexer.setTransactionService((TransactionServiceImpl)transactionService);
// authenticate
authenticationComponent.setSystemUserAsCurrentUser();

View File

@@ -76,4 +76,13 @@ public class DummyTransactionService implements TransactionService
{
return txn;
}
public RetryingTransactionHelper getRetryingTransactionHelper()
{
RetryingTransactionHelper helper = new RetryingTransactionHelper();
helper.setMaxRetries(20);
helper.setTransactionService(this);
helper.setReadOnly(false);
return helper;
}
}

View File

@@ -32,6 +32,7 @@ import javax.transaction.UserTransaction;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.error.ExceptionStackUtil;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.transaction.TransactionService;
import org.apache.log4j.Logger;
import org.hibernate.StaleObjectStateException;
@@ -48,6 +49,7 @@ import org.springframework.dao.DeadlockLoserDataAccessException;
*/
public class RetryingTransactionHelper
{
private static final String MSG_READ_ONLY = "permissions.err_read_only";
private static Logger fgLogger = Logger.getLogger(RetryingTransactionHelper.class);
/**
@@ -74,6 +76,11 @@ public class RetryingTransactionHelper
*/
private int fMaxRetries;
/**
* Whether the the transactions may only be reads
*/
private boolean readOnly;
/**
* Random number generator for retry delays.
*/
@@ -119,6 +126,14 @@ public class RetryingTransactionHelper
fMaxRetries = maxRetries;
}
/**
* Set whether this helper only supports read transactions.
*/
public void setReadOnly(boolean readOnly)
{
this.readOnly = readOnly;
}
/**
* Execute a callback in a transaction until it succeeds, fails
* because of an error not the result of an optimistic locking failure,
@@ -172,6 +187,10 @@ public class RetryingTransactionHelper
*/
public <R> R doInTransaction(RetryingTransactionCallback<R> cb, boolean readOnly, boolean newTransaction)
{
if (this.readOnly && !readOnly)
{
throw new AccessDeniedException(MSG_READ_ONLY);
}
// Track the last exception caught, so that we
// can throw it if we run out of retries.
RuntimeException lastException = null;
@@ -208,7 +227,9 @@ public class RetryingTransactionHelper
{
if (count != 0)
{
fgLogger.debug("Transaction succeeded after " + count + " retries");
fgLogger.debug(
"Transaction succeeded after " + count +
" retries on thread " + Thread.currentThread().getName());
}
}
return result;

View File

@@ -36,10 +36,11 @@ import org.springframework.transaction.TransactionDefinition;
*
* @author David Caruana
*/
public class TransactionComponent implements TransactionService
public class TransactionServiceImpl implements TransactionService
{
private PlatformTransactionManager transactionManager;
private boolean readOnly = false;
private int maxRetries = 20;
/**
* Set the transaction manager to use
@@ -65,6 +66,17 @@ public class TransactionComponent implements TransactionService
{
return readOnly;
}
/**
* Set the maximum number of retries that will be done by the
* {@link RetryingTransactionHelper transaction helper}.
*
* @param maxRetries the maximum transaction retries
*/
public void setMaxRetries(int maxRetries)
{
this.maxRetries = maxRetries;
}
/**
* @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED
@@ -121,4 +133,16 @@ public class TransactionComponent implements TransactionService
TransactionDefinition.TIMEOUT_DEFAULT);
return txn;
}
/**
* Creates a new helper instance. It can be reused.
*/
public RetryingTransactionHelper getRetryingTransactionHelper()
{
RetryingTransactionHelper helper = new RetryingTransactionHelper();
helper.setMaxRetries(maxRetries);
helper.setTransactionService(this);
helper.setReadOnly(readOnly);
return helper;
}
}

View File

@@ -30,6 +30,8 @@ import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.util.ApplicationContextHelper;
@@ -38,24 +40,24 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @see org.alfresco.repo.transaction.TransactionComponent
* @see org.alfresco.repo.transaction.TransactionServiceImpl
*
* @author Derek Hulley
*/
public class TransactionComponentTest extends TestCase
public class TransactionServiceImplTest extends TestCase
{
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private PlatformTransactionManager transactionManager;
private TransactionComponent transactionComponent;
private TransactionServiceImpl transactionService;
private NodeService nodeService;
public void setUp() throws Exception
{
transactionManager = (PlatformTransactionManager) ctx.getBean("transactionManager");
transactionComponent = new TransactionComponent();
transactionComponent.setTransactionManager(transactionManager);
transactionComponent.setAllowWrite(true);
transactionService = new TransactionServiceImpl();
transactionService.setTransactionManager(transactionManager);
transactionService.setAllowWrite(true);
nodeService = (NodeService) ctx.getBean("dbNodeService");
}
@@ -63,12 +65,12 @@ public class TransactionComponentTest extends TestCase
public void testPropagatingTxn() throws Exception
{
// start a transaction
UserTransaction txnOuter = transactionComponent.getUserTransaction();
UserTransaction txnOuter = transactionService.getUserTransaction();
txnOuter.begin();
String txnIdOuter = AlfrescoTransactionSupport.getTransactionId();
// start a propagating txn
UserTransaction txnInner = transactionComponent.getUserTransaction();
UserTransaction txnInner = transactionService.getUserTransaction();
txnInner.begin();
String txnIdInner = AlfrescoTransactionSupport.getTransactionId();
@@ -97,12 +99,12 @@ public class TransactionComponentTest extends TestCase
public void testNonPropagatingTxn() throws Exception
{
// start a transaction
UserTransaction txnOuter = transactionComponent.getUserTransaction();
UserTransaction txnOuter = transactionService.getUserTransaction();
txnOuter.begin();
String txnIdOuter = AlfrescoTransactionSupport.getTransactionId();
// start a propagating txn
UserTransaction txnInner = transactionComponent.getNonPropagatingUserTransaction();
UserTransaction txnInner = transactionService.getNonPropagatingUserTransaction();
txnInner.begin();
String txnIdInner = AlfrescoTransactionSupport.getTransactionId();
@@ -119,9 +121,9 @@ public class TransactionComponentTest extends TestCase
public void testReadOnlyTxn() throws Exception
{
// start a read-only transaction
transactionComponent.setAllowWrite(false);
transactionService.setAllowWrite(false);
UserTransaction txn = transactionComponent.getUserTransaction();
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
// do some writing
@@ -135,8 +137,39 @@ public class TransactionComponentTest extends TestCase
}
catch (InvalidDataAccessApiUsageException e)
{
@SuppressWarnings("unused")
int i = 0;
// expected
}
}
public void testGetRetryingTransactionHelper()
{
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
return null;
}
};
assertFalse("Retriers must be new instances",
transactionService.getRetryingTransactionHelper() == transactionService.getRetryingTransactionHelper());
transactionService.setAllowWrite(true);
transactionService.getRetryingTransactionHelper().doInTransaction(callback, true);
transactionService.getRetryingTransactionHelper().doInTransaction(callback, false);
transactionService.setAllowWrite(false);
transactionService.getRetryingTransactionHelper().doInTransaction(callback, true);
try
{
transactionService.getRetryingTransactionHelper().doInTransaction(callback, false);
fail("Expected AccessDeniedException when starting to write to a read-only transaction service.");
}
catch (AccessDeniedException e)
{
// Expected
}
}
}

View File

@@ -256,7 +256,7 @@ public class VersionServiceImplTest extends BaseVersionStoreTest
* Test revert
*/
@SuppressWarnings("unused")
public void xtestRevert()
public void testRevert()
{
// Create a versionable node
NodeRef versionableNode = createNewVersionableNode();

View File

@@ -26,6 +26,7 @@ package org.alfresco.service.transaction;
import javax.transaction.UserTransaction;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.service.NotAuditable;
import org.alfresco.service.PublicService;
@@ -96,4 +97,14 @@ public interface TransactionService
*/
@NotAuditable
UserTransaction getNonPropagatingUserTransaction(boolean readOnly);
/**
* Get the standard instance of the helper object that supports transaction retrying.
*
* @return
* Returns a helper object that executes units of work transactionally. The helper
* can be reused or altered as required.
*/
@NotAuditable
RetryingTransactionHelper getRetryingTransactionHelper();
}