Turned off pessimistic locking as now, for some reason I can't fathom, they

don't seem to prevent deadlocks.  Also made HibernateTxn derive from 
HibernateTemplate, mostly to be able to sort out recoverable and non-recoverable
DB exceptions.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3279 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2006-07-04 17:05:12 +00:00
parent b05422171a
commit 30f1779a95
3 changed files with 39 additions and 45 deletions

View File

@@ -58,8 +58,8 @@ abstract class DirectoryNodeImpl extends AVMNodeImpl implements DirectoryNode
{ {
// TODO This may be very Dangerous! // TODO This may be very Dangerous!
Session sess = SuperRepository.GetInstance().getSession(); Session sess = SuperRepository.GetInstance().getSession();
ChildEntry entry = (ChildEntry)sess.get(ChildEntryImpl.class, new ChildEntryImpl(name, this, null), ChildEntry entry = (ChildEntry)sess.get(ChildEntryImpl.class, new ChildEntryImpl(name, this, null));
(write && getIsNew()) ? LockMode.UPGRADE : LockMode.READ); // (write && getIsNew()) ? LockMode.UPGRADE : LockMode.READ);
return entry; return entry;
} }

View File

@@ -440,7 +440,6 @@ class SuperRepository
for (String repName : repositories) for (String repName : repositories)
{ {
Repository repo = getRepositoryByName(repName, true); Repository repo = getRepositoryByName(repName, true);
fSession.get().lock(repo, LockMode.UPGRADE);
// fSession.get().lock(repo, LockMode.UPGRADE); // fSession.get().lock(repo, LockMode.UPGRADE);
result.add(repo.createSnapshot()); result.add(repo.createSnapshot());
} }
@@ -455,7 +454,7 @@ class SuperRepository
public int createSnapshot(String repository) public int createSnapshot(String repository)
{ {
Repository repo = getRepositoryByName(repository, true); Repository repo = getRepositoryByName(repository, true);
fSession.get().lock(repo, LockMode.UPGRADE); // fSession.get().lock(repo, LockMode.UPGRADE);
// fSession.get().lock(repo, LockMode.UPGRADE); // fSession.get().lock(repo, LockMode.UPGRADE);
return repo.createSnapshot(); return repo.createSnapshot();
} }
@@ -716,8 +715,8 @@ class SuperRepository
private Repository getRepositoryByName(String name, boolean write) private Repository getRepositoryByName(String name, boolean write)
{ {
Repository rep = (Repository)fSession.get().get(RepositoryImpl.class, Repository rep = (Repository)fSession.get().get(RepositoryImpl.class,
name, LockMode.READ /*, name); /* LockMode.READ ,
write ? LockMode.UPGRADE : LockMode.READ*/); write ? LockMode.UPGRADE : LockMode.READ); */
if (rep == null) if (rep == null)
{ {
throw new AVMNotFoundException("Repository not found: " + name); throw new AVMNotFoundException("Repository not found: " + name);

View File

@@ -22,6 +22,7 @@ import java.util.Random;
import org.alfresco.repo.avm.AVMException; import org.alfresco.repo.avm.AVMException;
import org.alfresco.repo.avm.AVMNotFoundException; import org.alfresco.repo.avm.AVMNotFoundException;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.ObjectNotFoundException; import org.hibernate.ObjectNotFoundException;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@@ -29,12 +30,16 @@ import org.hibernate.StaleStateException;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.exception.GenericJDBCException; import org.hibernate.exception.GenericJDBCException;
import org.hibernate.exception.LockAcquisitionException; import org.hibernate.exception.LockAcquisitionException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.orm.hibernate3.HibernateTemplate;
/** /**
* Helper for DAOs. * Helper for DAOs.
* @author britt * @author britt
*/ */
public class HibernateTxn public class HibernateTxn extends HibernateTemplate
{ {
/** /**
* The SessionFactory. * The SessionFactory.
@@ -61,6 +66,7 @@ public class HibernateTxn
*/ */
public void setSessionFactory(SessionFactory factory) public void setSessionFactory(SessionFactory factory)
{ {
super.setSessionFactory(factory);
fSessionFactory = factory; fSessionFactory = factory;
} }
@@ -97,59 +103,48 @@ public class HibernateTxn
{ {
// Do nothing. // Do nothing.
} }
// If we've lost a race or we've deadlocked, retry. // Translate the exception.
if (t instanceof StaleStateException || if (t instanceof JDBCException)
t instanceof GenericJDBCException ||
t instanceof LockAcquisitionException)
{ {
if (t instanceof StaleStateException) t = convertJdbcAccessException((JDBCException)t);
}
else if (t instanceof HibernateException)
{
t = convertHibernateAccessException((HibernateException)t);
}
// If we've lost a race or we've deadlocked, retry.
if (t instanceof DeadlockLoserDataAccessException)
{
try
{ {
// System.err.println("Lost Race"); long interval;
// StackTraceElement [] stack = t.getStackTrace(); synchronized (fRandom)
// long threadID = Thread.currentThread().getId(); {
// for (StackTraceElement frame : stack) interval = fRandom.nextInt(1000);
// { }
// System.err.println(threadID + " " + frame); Thread.sleep(interval);
// } continue;
} }
else catch (InterruptedException ie)
{ {
// System.err.println("Deadlock"); // Do nothing.
// StackTraceElement [] stack = t.getStackTrace();
// long threadID = Thread.currentThread().getId();
// for (StackTraceElement frame : stack)
// {
// System.err.println(threadID + " " + frame);
// }
try
{
long interval;
synchronized (fRandom)
{
interval = fRandom.nextInt(1000);
}
Thread.sleep(interval);
continue;
}
catch (InterruptedException ie)
{
// Do nothing.
}
} }
continue; continue;
} }
if (t instanceof OptimisticLockingFailureException)
{
continue;
}
} }
if (t instanceof AVMException) if (t instanceof AVMException)
{ {
throw (AVMException)t; throw (AVMException)t;
} }
// TODO Crack t into more useful exception types. if (t instanceof DataRetrievalFailureException)
// Otherwise nothing we can do except throw.
if (t instanceof ObjectNotFoundException)
{ {
throw new AVMNotFoundException("Object not found.", t); throw new AVMNotFoundException("Object not found.", t);
} }
throw new AVMException("Unrecoverable error."); throw new AVMException("Unrecoverable error.", t);
} }
finally finally
{ {