mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Fixed ALF-1562: AuditModelRegistry and audit loading can fail when started on demand
- Transactional caches in nested transactions force the outer txn caches to drop new data - Forces a requery of transactional data in case the inner transaction caused it to go stale git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@18868 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -163,6 +163,41 @@ public class CacheTest extends TestCase
|
|||||||
txnHelper.doInTransaction(callback);
|
txnHelper.doInTransaction(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure that an outer transaction gets to see cached data modified by an inner transaction.
|
||||||
|
*/
|
||||||
|
public void testOuterTransactionStaleDataAvoidance() throws Throwable
|
||||||
|
{
|
||||||
|
final TransactionService transactionService = serviceRegistry.getTransactionService();
|
||||||
|
|
||||||
|
RetryingTransactionCallback<Void> outer = new RetryingTransactionCallback<Void>()
|
||||||
|
{
|
||||||
|
public Void execute() throws Throwable
|
||||||
|
{
|
||||||
|
// Put a value in the cache
|
||||||
|
transactionalCache.put("A", "OUTER");
|
||||||
|
assertNotNull("Expected to get a value before inner txn.", transactionalCache.get("A"));
|
||||||
|
|
||||||
|
RetryingTransactionCallback<Void> inner = new RetryingTransactionCallback<Void>()
|
||||||
|
{
|
||||||
|
public Void execute() throws Throwable
|
||||||
|
{
|
||||||
|
// Just update the cache
|
||||||
|
transactionalCache.put("A", "INNER");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Do it in a nested txn
|
||||||
|
transactionService.getRetryingTransactionHelper().doInTransaction(inner, false, true);
|
||||||
|
|
||||||
|
assertNull("Expected to *not* get a value after inner txn.", transactionalCache.get("A"));
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
transactionService.getRetryingTransactionHelper().doInTransaction(outer);
|
||||||
|
}
|
||||||
|
|
||||||
public void testTransactionalCacheWithSingleTxn() throws Throwable
|
public void testTransactionalCacheWithSingleTxn() throws Throwable
|
||||||
{
|
{
|
||||||
String newGlobalOne = "new_global_one";
|
String newGlobalOne = "new_global_one";
|
||||||
@@ -345,7 +380,7 @@ public class CacheTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests a straight Ehcache adapter against a transactional cache both in and out
|
* Tests a straight Ehcache adapter against a transactional cache both in and outprojects/repository/source/java/org/alfresco/repo/cache/TransactionalCache.java
|
||||||
* of a transaction. This is done repeatedly, pushing the count up.
|
* of a transaction. This is done repeatedly, pushing the count up.
|
||||||
*/
|
*/
|
||||||
public void testPerformance() throws Exception
|
public void testPerformance() throws Exception
|
||||||
|
@@ -28,6 +28,7 @@ import java.io.Serializable;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import net.sf.ehcache.Cache;
|
import net.sf.ehcache.Cache;
|
||||||
import net.sf.ehcache.CacheException;
|
import net.sf.ehcache.CacheException;
|
||||||
@@ -97,6 +98,9 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
|
|
||||||
/** a unique string identifying this instance when binding resources */
|
/** a unique string identifying this instance when binding resources */
|
||||||
private String resourceKeyTxnData;
|
private String resourceKeyTxnData;
|
||||||
|
|
||||||
|
/** Keep track of what transactions have been up to before */
|
||||||
|
private ThreadLocal<Set<TransactionData>> threadTxnData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public constructor.
|
* Public constructor.
|
||||||
@@ -105,6 +109,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
{
|
{
|
||||||
logger = LogFactory.getLog(TransactionalCache.class);
|
logger = LogFactory.getLog(TransactionalCache.class);
|
||||||
isDebugEnabled = logger.isDebugEnabled();
|
isDebugEnabled = logger.isDebugEnabled();
|
||||||
|
threadTxnData = new ThreadLocal<Set<TransactionData>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -208,7 +213,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
if (data == null)
|
if (data == null)
|
||||||
{
|
{
|
||||||
String txnId = AlfrescoTransactionSupport.getTransactionId();
|
String txnId = AlfrescoTransactionSupport.getTransactionId();
|
||||||
data = new TransactionData();
|
data = new TransactionData(txnId);
|
||||||
// create and initialize caches
|
// create and initialize caches
|
||||||
data.updatedItemsCache = new Cache(
|
data.updatedItemsCache = new Cache(
|
||||||
name + "_"+ txnId + "_updates",
|
name + "_"+ txnId + "_updates",
|
||||||
@@ -216,6 +221,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
data.removedItemsCache = new Cache(
|
data.removedItemsCache = new Cache(
|
||||||
name + "_" + txnId + "_removes",
|
name + "_" + txnId + "_removes",
|
||||||
maxCacheSize, false, true, 0, 0);
|
maxCacheSize, false, true, 0, 0);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cacheManager.addCache(data.updatedItemsCache);
|
cacheManager.addCache(data.updatedItemsCache);
|
||||||
@@ -232,6 +238,15 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
AlfrescoTransactionSupport.bindListener(this);
|
AlfrescoTransactionSupport.bindListener(this);
|
||||||
}
|
}
|
||||||
AlfrescoTransactionSupport.bindResource(resourceKeyTxnData, data);
|
AlfrescoTransactionSupport.bindResource(resourceKeyTxnData, data);
|
||||||
|
|
||||||
|
// Put the data into the set for potential inner transaction usage
|
||||||
|
Set<TransactionData> threadSet = threadTxnData.get();
|
||||||
|
if (threadSet == null)
|
||||||
|
{
|
||||||
|
threadSet = new HashSet<TransactionData>(3);
|
||||||
|
threadTxnData.set(threadSet);
|
||||||
|
}
|
||||||
|
threadSet.add(data); // NB: This is removed during commit or rollback
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -691,6 +706,15 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
removeCaches(txnData);
|
removeCaches(txnData);
|
||||||
|
|
||||||
|
// Remove our txnData entry from the thread
|
||||||
|
Set<TransactionData> threadSet = threadTxnData.get();
|
||||||
|
threadSet.remove(txnData);
|
||||||
|
// We pessimistically kill all new data cached by other transactions
|
||||||
|
for (TransactionData outerTxnData : threadSet)
|
||||||
|
{
|
||||||
|
outerTxnData.removeUpdates();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -702,6 +726,10 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
TransactionData txnData = getTransactionData();
|
TransactionData txnData = getTransactionData();
|
||||||
// drop caches from cachemanager
|
// drop caches from cachemanager
|
||||||
removeCaches(txnData);
|
removeCaches(txnData);
|
||||||
|
|
||||||
|
// Remove our txnData entry from the thread
|
||||||
|
Set<TransactionData> threadSet = threadTxnData.get();
|
||||||
|
threadSet.remove(txnData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -858,10 +886,57 @@ public class TransactionalCache<K extends Serializable, V extends Object>
|
|||||||
/** Data holder to bind data to the transaction */
|
/** Data holder to bind data to the transaction */
|
||||||
private class TransactionData
|
private class TransactionData
|
||||||
{
|
{
|
||||||
|
/** A thread-locally unique ID */
|
||||||
|
private String id;
|
||||||
|
|
||||||
private Cache updatedItemsCache;
|
private Cache updatedItemsCache;
|
||||||
private Cache removedItemsCache;
|
private Cache removedItemsCache;
|
||||||
private boolean haveIssuedFullWarning;
|
private boolean haveIssuedFullWarning;
|
||||||
private boolean isClearOn;
|
private boolean isClearOn;
|
||||||
private boolean isClosed;
|
private boolean isClosed;
|
||||||
|
|
||||||
|
private TransactionData(String id)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("TransactionData [id=").append(id).append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode()
|
||||||
|
{
|
||||||
|
return id.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controlled implementation of equals i.e. we don't check all conditions; the containing
|
||||||
|
* class needs to ensure types don't get mixed in sets, etc.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj)
|
||||||
|
{
|
||||||
|
if (this == obj)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
TransactionData other = (TransactionData) obj;
|
||||||
|
return id.equals(other.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pessimistic method that removes all updates and additions from the local
|
||||||
|
* transactional data. This method is used when inner transactions are started
|
||||||
|
* that modify the cache.
|
||||||
|
*/
|
||||||
|
private void removeUpdates()
|
||||||
|
{
|
||||||
|
updatedItemsCache.removeAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user