Steven Glover f14db280f3 Fix for http://issues.alfresco.com/jira/browse/ALF-713.
Can't reproduce the bug but spotted a race condition in UserUsageTrackingComponent that may cause this in rare circumstances. Deal with this by throwing a ConcurrencyFailureException and let the txn retry handler retry the operation.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19862 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2010-04-15 12:38:19 +00:00

251 lines
7.9 KiB
Java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.domain.hibernate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.alfresco.repo.domain.Node;
import org.alfresco.repo.domain.UsageDelta;
import org.alfresco.repo.domain.UsageDeltaDAO;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.transaction.TransactionalDao;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.util.GUID;
import org.alfresco.util.Pair;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.extensions.surf.util.ParameterCheck;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Hibernate-specific implementation of the persistence-independent <b>Usage Delta</b> DAO interface
*
*/
public class HibernateUsageDeltaDAO extends HibernateDaoSupport implements UsageDeltaDAO, TransactionalDao
{
// private static final String QUERY_GET_DELTAS = "usage.GetDeltas";
private static final String QUERY_GET_TOTAL_DELTA_SIZE = "usage.GetTotalDeltaSize";
private static final String QUERY_GET_USAGE_DELTA_NODES = "usage.GetUsageDeltaNodes";
private static final String QUERY_DELETE_DELTAS_FOR_NODE = "usage.DeleteUsageDeltasForNode";
/** a uuid identifying this unique instance */
private final String uuid;
private NodeDaoService nodeDaoService;
public void setNodeDaoService(NodeDaoService nodeDaoService)
{
this.nodeDaoService = nodeDaoService;
}
/**
*
*/
public HibernateUsageDeltaDAO()
{
this.uuid = GUID.generate();
}
/**
* Checks equality by type and uuid
*/
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
else if (!(obj instanceof HibernateUsageDeltaDAO))
{
return false;
}
HibernateUsageDeltaDAO that = (HibernateUsageDeltaDAO) obj;
return this.uuid.equals(that.uuid);
}
/**
* @see #uuid
*/
public int hashCode()
{
return uuid.hashCode();
}
/**
* NO-OP
*/
public void beforeCommit()
{
}
/**
* Does this <tt>Session</tt> contain any changes which must be
* synchronized with the store?
*
* @return true => changes are pending
*/
public boolean isDirty()
{
// create a callback for the task
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
return session.isDirty();
}
};
// execute the callback
return ((Boolean)getHibernateTemplate().execute(callback)).booleanValue();
}
/**
* Just flushes the session
*/
public void flush()
{
getSession().flush();
}
private Long getNodeIdNotNull(NodeRef nodeRef) throws InvalidNodeRefException
{
ParameterCheck.mandatory("nodeRef", nodeRef);
Pair<Long, NodeRef> nodePair = nodeDaoService.getNodePair(nodeRef);
if (nodePair == null)
{
throw new InvalidNodeRefException("Node does not exist: " + nodeRef, nodeRef);
}
return nodePair.getFirst();
}
public int deleteDeltas(NodeRef nodeRef)
{
Long nodeId = getNodeIdNotNull(nodeRef);
return deleteDeltas(nodeId);
}
@SuppressWarnings("unchecked")
public int deleteDeltas(final Long nodeId)
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(QUERY_DELETE_DELTAS_FOR_NODE);
query.setParameter("nodeId", nodeId);
return query.executeUpdate();
}
};
// execute
Integer delCount = (Integer) getHibernateTemplate().execute(callback);
return (delCount == null ? 0 : delCount.intValue());
}
@SuppressWarnings("unchecked")
private Object[] getTotalDeltaSizeImpl(NodeRef nodeRef)
{
final Long nodeId = getNodeIdNotNull(nodeRef);
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(QUERY_GET_TOTAL_DELTA_SIZE);
query.setParameter("nodeId", nodeId);
query.setReadOnly(true);
return query.uniqueResult();
}
};
// execute read-only tx
return (Object[])getHibernateTemplate().execute(callback);
}
public long getTotalDeltaSize(NodeRef nodeRef)
{
Object[] result = getTotalDeltaSizeImpl(nodeRef);
Long queryResult = (Long)result[1];
return (queryResult == null ? 0 : queryResult.longValue());
}
/**
* Guard against deleting deltas committed by another transaction after calculating the delta sum above.
* If the expected number of deletes is different from the actual number of deletes then deltas from
* another committed transaction are probably being removed.
*/
@SuppressWarnings("unchecked")
public long getAndRemoveTotalDeltaSize(NodeRef nodeRef)
{
Object[] result = getTotalDeltaSizeImpl(nodeRef);
Long queryResult = (Long)result[1];
Number deltaCount = (Number)result[0];
long expectedNumDeletes = deltaCount == null ? 0 : deltaCount.intValue();
int numDeltasRemoved = deleteDeltas(nodeRef);
if(expectedNumDeletes != numDeltasRemoved) {
throw new ConcurrencyFailureException("More than expected number of usage delta deletes has occurred for node id "
+ nodeRef.getId() + " in store " + nodeRef.getStoreRef());
}
return (queryResult == null ? 0 : queryResult.longValue());
}
public void insertDelta(NodeRef usageNodeRef, long deltaSize)
{
Long nodeId = getNodeIdNotNull(usageNodeRef);
Node node = (Node) getHibernateTemplate().get(NodeImpl.class, nodeId);
UsageDelta delta = new UsageDeltaImpl();
// delta properties
delta.setNode(node);
delta.setDeltaSize(deltaSize);
// Save
getSession().save(delta);
}
@SuppressWarnings("unchecked")
public Set<NodeRef> getUsageDeltaNodes()
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(QUERY_GET_USAGE_DELTA_NODES);
query.setReadOnly(true);
return query.list();
}
};
// execute read-only tx
List<Node> queryResults = (List<Node>)getHibernateTemplate().execute(callback);
Set<NodeRef> results = new HashSet<NodeRef>(queryResults.size(), 1.0F);
for (Node node : queryResults)
{
results.add(node.getNodeRef());
}
return results;
}
}