mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Hibernate parts of AVM now based on Spring PlatformTransactionManager
abstraction. Configurations to match. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3286 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.avm.hibernate;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.springframework.orm.hibernate3.HibernateCallback;
|
||||
|
||||
/**
|
||||
* This is a wrapper around HibernateTxnCallback implementation.
|
||||
* @author britt
|
||||
*/
|
||||
public class HibernateCallbackWrapper implements HibernateCallback
|
||||
{
|
||||
/**
|
||||
* The HibernateTxnCallback to execute.
|
||||
*/
|
||||
private HibernateTxnCallback fCallback;
|
||||
|
||||
/**
|
||||
* Make one up.
|
||||
* @param callback
|
||||
*/
|
||||
public HibernateCallbackWrapper(HibernateTxnCallback callback)
|
||||
{
|
||||
fCallback = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the HibernateTxnCallback internally.
|
||||
* @param session The Hibernate Session.
|
||||
*/
|
||||
public Object doInHibernate(Session session) throws HibernateException,
|
||||
SQLException
|
||||
{
|
||||
fCallback.perform(session);
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -21,15 +21,14 @@ import java.util.Random;
|
||||
|
||||
import org.alfresco.repo.avm.AVMException;
|
||||
import org.alfresco.repo.avm.AVMNotFoundException;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.JDBCException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.dao.DeadlockLoserDataAccessException;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.orm.hibernate3.HibernateTemplate;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
|
||||
/**
|
||||
* Helper for DAOs.
|
||||
@@ -38,9 +37,19 @@ import org.springframework.orm.hibernate3.HibernateTemplate;
|
||||
public class HibernateTxn extends HibernateTemplate
|
||||
{
|
||||
/**
|
||||
* The SessionFactory.
|
||||
* The transaction manager.
|
||||
*/
|
||||
private SessionFactory fSessionFactory;
|
||||
private PlatformTransactionManager fTransactionManager;
|
||||
|
||||
/**
|
||||
* The read transaction definition.
|
||||
*/
|
||||
private TransactionDefinition fReadDefinition;
|
||||
|
||||
/**
|
||||
* The write transaction definition.
|
||||
*/
|
||||
private TransactionDefinition fWriteDefinition;
|
||||
|
||||
/**
|
||||
* The random number generator for inter-retry sleep.
|
||||
@@ -56,16 +65,6 @@ public class HibernateTxn extends HibernateTemplate
|
||||
fRandom = new Random();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Hibernate Session factory.
|
||||
* @param factory
|
||||
*/
|
||||
public void setSessionFactory(SessionFactory factory)
|
||||
{
|
||||
super.setSessionFactory(factory);
|
||||
fSessionFactory = factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a set of operations under a single Hibernate transaction.
|
||||
* Keep trying if the operation fails because of a concurrency issue.
|
||||
@@ -74,44 +73,34 @@ public class HibernateTxn extends HibernateTemplate
|
||||
*/
|
||||
public void perform(HibernateTxnCallback callback, boolean write)
|
||||
{
|
||||
Session sess = null;
|
||||
Transaction txn = null;
|
||||
while (true)
|
||||
{
|
||||
TransactionStatus status = null;
|
||||
try
|
||||
{
|
||||
sess = fSessionFactory.openSession();
|
||||
txn = sess.beginTransaction();
|
||||
callback.perform(sess);
|
||||
txn.commit();
|
||||
status =
|
||||
fTransactionManager.getTransaction(write ? fWriteDefinition : fReadDefinition);
|
||||
execute(new HibernateCallbackWrapper(callback));
|
||||
try
|
||||
{
|
||||
fTransactionManager.commit(status);
|
||||
}
|
||||
catch (TransactionException te)
|
||||
{
|
||||
throw new AVMException("Transaction Exception.", te);
|
||||
}
|
||||
return;
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
// TODO Add appropriate logging.
|
||||
if (txn != null)
|
||||
if (!status.isCompleted())
|
||||
{
|
||||
try
|
||||
{
|
||||
txn.rollback();
|
||||
}
|
||||
catch (HibernateException he)
|
||||
{
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
// Translate the exception.
|
||||
if (t instanceof JDBCException)
|
||||
{
|
||||
t = convertJdbcAccessException((JDBCException)t);
|
||||
}
|
||||
else if (t instanceof HibernateException)
|
||||
{
|
||||
t = convertHibernateAccessException((HibernateException)t);
|
||||
fTransactionManager.rollback(status);
|
||||
}
|
||||
// If we've lost a race or we've deadlocked, retry.
|
||||
if (t instanceof DeadlockLoserDataAccessException)
|
||||
{
|
||||
System.err.println("Deadlock.");
|
||||
try
|
||||
{
|
||||
long interval;
|
||||
@@ -120,7 +109,6 @@ public class HibernateTxn extends HibernateTemplate
|
||||
interval = fRandom.nextInt(1000);
|
||||
}
|
||||
Thread.sleep(interval);
|
||||
continue;
|
||||
}
|
||||
catch (InterruptedException ie)
|
||||
{
|
||||
@@ -130,6 +118,7 @@ public class HibernateTxn extends HibernateTemplate
|
||||
}
|
||||
if (t instanceof OptimisticLockingFailureException)
|
||||
{
|
||||
System.err.println("Lost Race.");
|
||||
continue;
|
||||
}
|
||||
if (t instanceof AVMException)
|
||||
@@ -138,24 +127,38 @@ public class HibernateTxn extends HibernateTemplate
|
||||
}
|
||||
if (t instanceof DataRetrievalFailureException)
|
||||
{
|
||||
System.err.println("Data Retrieval Error.");
|
||||
throw new AVMNotFoundException("Object not found.", t);
|
||||
}
|
||||
throw new AVMException("Unrecoverable error.", t);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sess != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
sess.close();
|
||||
}
|
||||
catch (HibernateException he)
|
||||
{
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the transaction manager we are operating under.
|
||||
* @param manager
|
||||
*/
|
||||
public void setTransactionManager(PlatformTransactionManager manager)
|
||||
{
|
||||
fTransactionManager = manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the read Transaction Definition.
|
||||
* @param def
|
||||
*/
|
||||
public void setReadTransactionDefinition(TransactionDefinition def)
|
||||
{
|
||||
fReadDefinition = def;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the write Transaction Definition.
|
||||
* @param def
|
||||
*/
|
||||
public void setWriteTransactionDefinition(TransactionDefinition def)
|
||||
{
|
||||
fWriteDefinition = def;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user