Checkpoint: RetryingTransactionHelper, which executes a unit of work in a UserTransaction

with retries for transient failures.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4573 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2006-12-11 19:57:20 +00:00
parent f80dcdd7e4
commit b4d49c2c3c
3 changed files with 219 additions and 41 deletions

View File

@@ -149,6 +149,15 @@
</property> </property>
</bean> </bean>
<bean id="retryingTransactionHelper" class="org.alfresco.repo.transaction.RetryingTransactionHelper">
<property name="transactionService">
<ref bean="transactionComponent"/>
</property>
<property name="maxRetries">
<value>10</value>
</property>
</bean>
<!-- --> <!-- -->
<!-- I18N --> <!-- I18N -->
<!-- --> <!-- -->

View File

@@ -19,6 +19,7 @@ package org.alfresco.repo.avm;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Serializable; import java.io.Serializable;
@@ -40,6 +41,7 @@ import org.alfresco.repo.avm.actions.SimpleAVMSubmitAction;
import org.alfresco.repo.avm.util.BulkLoader; import org.alfresco.repo.avm.util.BulkLoader;
import org.alfresco.repo.domain.PropertyValue; import org.alfresco.repo.domain.PropertyValue;
import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.TransactionUtil; import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.service.cmr.avm.AVMBadArgumentException; import org.alfresco.service.cmr.avm.AVMBadArgumentException;
import org.alfresco.service.cmr.avm.AVMCycleException; import org.alfresco.service.cmr.avm.AVMCycleException;
@@ -3262,9 +3264,11 @@ public class AVMServiceTest extends AVMServiceTestBase
try try
{ {
setupBasicTree(); setupBasicTree();
class TxnWork implements TransactionUtil.TransactionWork<Object> class TxnCallback implements RetryingTransactionHelper.Callback
{ {
public Object doWork() throws Exception public Object execute()
{
try
{ {
AVMService service = (AVMService)fContext.getBean("avmService"); AVMService service = (AVMService)fContext.getBean("avmService");
service.createLayeredDirectory("main:/a", "main:/", "layer"); service.createLayeredDirectory("main:/a", "main:/", "layer");
@@ -3309,9 +3313,18 @@ public class AVMServiceTest extends AVMServiceTestBase
service.createFile("main:/layer/b/c/groo", "granistan").close(); service.createFile("main:/layer/b/c/groo", "granistan").close();
return null; return null;
} }
catch (IOException e)
{
e.printStackTrace();
fail();
return null;
} }
TransactionUtil.executeInUserTransaction((TransactionService)fContext.getBean("transactionComponent"), }
new TxnWork()); }
RetryingTransactionHelper helper =
(RetryingTransactionHelper)fContext.getBean("retryingTransactionHelper");
helper.doInTransaction(new TxnCallback(), false);
assertNotNull(fService.lookup(-1, "main:/layer/b/c/groo"));
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -0,0 +1,156 @@
/**
*
*/
package org.alfresco.repo.transaction;
import java.util.Random;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.transaction.TransactionService;
import org.apache.log4j.Logger;
import org.hibernate.StaleObjectStateException;
import org.hibernate.exception.LockAcquisitionException;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DeadlockLoserDataAccessException;
/**
* A helper that runs a unit of work inside a UserTransaction,
* transparently retrying the unit of work if the cause of
* failure is an optimistic locking or deadlock condition.
* @author britt
*/
public class RetryingTransactionHelper
{
private static Logger fgLogger = Logger.getLogger(RetryingTransactionHelper.class);
/**
* Reference to the TransactionService instance.
*/
private TransactionService fTxnService;
/**
* The maximum number of retries. -1 for infinity.
*/
private int fMaxRetries;
/**
* Random number generator for retry delays.
*/
private Random fRandom;
/**
* Callback interface
* @author britt
*/
public interface Callback
{
public Object execute();
};
/**
* Default constructor.
*/
public RetryingTransactionHelper()
{
fRandom = new Random(System.currentTimeMillis());
}
// Setters.
/**
* Set the TransactionService.
*/
public void setTransactionService(TransactionService service)
{
fTxnService = service;
}
/**
* Set the maximimum number of retries. -1 for infinity.
*/
public void setMaxRetries(int maxRetries)
{
fMaxRetries = maxRetries;
}
/**
* Execute a callback in a transaction until it succeeds, fails
* because of an error not the result of an optimistic locking failure,
* or a deadlock loser failure, or until a maximum number of retries have
* been attempted. NB that this ignores transaction status and relies entirely
* on thrown exceptions to decide to rollback. Also this is non-reentrant, not
* to be called within an existing transaction.
* @param cb The callback containing the unit of work.
* @param readOnly Whether this is a read only transaction.
* @return The result of the unit of work.
*/
public Object doInTransaction(Callback cb, boolean readOnly)
{
RuntimeException lastException = null;
for (int count = 0; fMaxRetries < 0 || count < fMaxRetries; ++count)
{
UserTransaction txn = null;
try
{
txn = fTxnService.getNonPropagatingUserTransaction(readOnly);
txn.begin();
Object result = cb.execute();
txn.commit();
if (fgLogger.isDebugEnabled())
{
if (count != 0)
{
fgLogger.debug("Transaction succeeded after " + count + " retries");
}
}
return result;
}
catch (Exception e)
{
if (txn != null)
{
try
{
txn.rollback();
}
catch (IllegalStateException e1)
{
throw new AlfrescoRuntimeException("Failure during rollback.", e1);
}
catch (SecurityException e1)
{
throw new AlfrescoRuntimeException("Failure during rollback.", e1);
}
catch (SystemException e1)
{
throw new AlfrescoRuntimeException("Failure during rollback.", e1);
}
}
if (e instanceof ConcurrencyFailureException ||
e instanceof DeadlockLoserDataAccessException ||
e instanceof StaleObjectStateException ||
e instanceof LockAcquisitionException)
{
lastException = (RuntimeException)e;
try
{
Thread.sleep(fRandom.nextInt(500 * count + 500));
}
catch (InterruptedException ie)
{
// Do nothing.
}
continue;
}
if (e instanceof RuntimeException)
{
throw (RuntimeException)e;
}
throw new AlfrescoRuntimeException("Exception in Transaction.", e);
}
}
throw lastException;
}
}