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:
Derek Hulley
2010-02-25 23:46:34 +00:00
parent f8c7f7432d
commit 44d2fc6bec
2 changed files with 112 additions and 2 deletions

View File

@@ -28,6 +28,7 @@ import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.sf.ehcache.Cache;
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 */
private String resourceKeyTxnData;
/** Keep track of what transactions have been up to before */
private ThreadLocal<Set<TransactionData>> threadTxnData;
/**
* Public constructor.
@@ -105,6 +109,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
{
logger = LogFactory.getLog(TransactionalCache.class);
isDebugEnabled = logger.isDebugEnabled();
threadTxnData = new ThreadLocal<Set<TransactionData>>();
}
/**
@@ -208,7 +213,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
if (data == null)
{
String txnId = AlfrescoTransactionSupport.getTransactionId();
data = new TransactionData();
data = new TransactionData(txnId);
// create and initialize caches
data.updatedItemsCache = new Cache(
name + "_"+ txnId + "_updates",
@@ -216,6 +221,7 @@ public class TransactionalCache<K extends Serializable, V extends Object>
data.removedItemsCache = new Cache(
name + "_" + txnId + "_removes",
maxCacheSize, false, true, 0, 0);
try
{
cacheManager.addCache(data.updatedItemsCache);
@@ -232,6 +238,15 @@ public class TransactionalCache<K extends Serializable, V extends Object>
AlfrescoTransactionSupport.bindListener(this);
}
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;
}
@@ -691,6 +706,15 @@ public class TransactionalCache<K extends Serializable, V extends Object>
finally
{
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();
// drop caches from cachemanager
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 */
private class TransactionData
{
/** A thread-locally unique ID */
private String id;
private Cache updatedItemsCache;
private Cache removedItemsCache;
private boolean haveIssuedFullWarning;
private boolean isClearOn;
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();
}
}
}