mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-06-02 17:35:18 +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:
parent
c54b07f607
commit
cdef918e07
@ -14,7 +14,7 @@
|
||||
</bean>
|
||||
|
||||
<!-- A read only DefaultTransactionDefinition -->
|
||||
<bean id="readOnlyTransactionDefinition"
|
||||
<bean id="readTransactionDefinition"
|
||||
class="org.springframework.transaction.support.DefaultTransactionDefinition">
|
||||
<property name="propagationBehaviorName">
|
||||
<value>PROPAGATION_REQUIRES_NEW</value>
|
||||
@ -92,13 +92,33 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- The Hibernate Transaction wrapper. -->
|
||||
<bean id="hibernateTxn" class="org.alfresco.repo.avm.hibernate.HibernateTxn">
|
||||
<!-- The HibernateTransactionManager -->
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="dataSource">
|
||||
<ref bean="dataSource"/>
|
||||
</property>
|
||||
<property name="sessionFactory">
|
||||
<ref bean="sessionFactory"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- The Hibernate Transaction wrapper. -->
|
||||
<bean id="hibernateTxn" class="org.alfresco.repo.avm.hibernate.HibernateTxn">
|
||||
<property name="sessionFactory">
|
||||
<ref bean="sessionFactory"/>
|
||||
</property>
|
||||
<property name="transactionManager">
|
||||
<ref bean="transactionManager"/>
|
||||
</property>
|
||||
<property name="readTransactionDefinition">
|
||||
<ref bean="readTransactionDefinition"/>
|
||||
</property>
|
||||
<property name="writeTransactionDefinition">
|
||||
<ref bean="writeTransactionDefinition"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="orphanReaper" class="org.alfresco.repo.avm.OrphanReaper"
|
||||
init-method="init" destroy-method="shutDown" depends-on="avmService">
|
||||
<property name="inactiveBaseSleep">
|
||||
|
@ -12,6 +12,6 @@ hibernate.connection.isolation=2
|
||||
hibernate.default_batch_fetch_size=16
|
||||
hibernate.jdbc.batch_versioned_data=true
|
||||
hibernate.cache.use_second_level_cache=true
|
||||
hibernate.hbm2ddl.auto=create
|
||||
hibernate.hbm2ddl.auto=update
|
||||
# AVM specific properties.
|
||||
avm.initialize=true
|
||||
avm.initialize=false
|
||||
|
@ -63,12 +63,60 @@
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- The HibernateTransactionManager -->
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="dataSource">
|
||||
<ref bean="dataSource"/>
|
||||
</property>
|
||||
<property name="sessionFactory">
|
||||
<ref bean="sessionFactory"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- A read only DefaultTransactionDefinition -->
|
||||
<bean id="readTransactionDefinition"
|
||||
class="org.springframework.transaction.support.DefaultTransactionDefinition">
|
||||
<property name="propagationBehaviorName">
|
||||
<value>PROPAGATION_REQUIRES_NEW</value>
|
||||
</property>
|
||||
<property name="isolationLevelName">
|
||||
<value>ISOLATION_READ_COMMITTED</value>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<value>true</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- A write DefaultTransactionDefinition -->
|
||||
<bean id="writeTransactionDefinition"
|
||||
class="org.springframework.transaction.support.DefaultTransactionDefinition">
|
||||
<property name="propagationBehaviorName">
|
||||
<value>PROPAGATION_REQUIRES_NEW</value>
|
||||
</property>
|
||||
<property name="isolationLevelName">
|
||||
<value>ISOLATION_READ_COMMITTED</value>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<value>false</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- The Hibernate Transaction wrapper. -->
|
||||
<bean id="hibernateTxn" class="org.alfresco.repo.avm.hibernate.HibernateTxn">
|
||||
<property name="sessionFactory">
|
||||
<ref bean="sessionFactory"/>
|
||||
</property>
|
||||
<property name="transactionManager">
|
||||
<ref bean="transactionManager"/>
|
||||
</property>
|
||||
<property name="readTransactionDefinition">
|
||||
<ref bean="readTransactionDefinition"/>
|
||||
</property>
|
||||
<property name="writeTransactionDefinition">
|
||||
<ref bean="writeTransactionDefinition"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="orphanReaper" class="org.alfresco.repo.avm.OrphanReaper"
|
||||
|
@ -8,7 +8,7 @@ db.pool.max=32
|
||||
# Hibernate properties
|
||||
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
|
||||
hibernate.current_session_context_class=thread
|
||||
hibernate.connection.isolation=2
|
||||
hibernate.connection.isolation=4
|
||||
hibernate.default_batch_fetch_size=16
|
||||
hibernate.jdbc.batch_versioned_data=true
|
||||
hibernate.cache.use_second_level_cache=true
|
||||
|
@ -956,7 +956,7 @@ public class AVMServiceImpl implements AVMService
|
||||
}
|
||||
}
|
||||
HTxnCallback doit = new HTxnCallback();
|
||||
fTransaction.perform(doit, false);
|
||||
fTransaction.perform(doit, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -261,9 +261,11 @@ public class OrphanReaper implements Runnable
|
||||
delete.setEntity("parent", node);
|
||||
delete.executeUpdate();
|
||||
}
|
||||
session.delete(node);
|
||||
}
|
||||
else if (node instanceof PlainFileNode)
|
||||
{
|
||||
session.delete(node);
|
||||
// FileContent should be purged if nobody else references it.
|
||||
FileContent content = ((PlainFileNode)node).getContent();
|
||||
if (content.getRefCount() == 1)
|
||||
@ -272,7 +274,10 @@ public class OrphanReaper implements Runnable
|
||||
session.delete(content);
|
||||
}
|
||||
}
|
||||
session.delete(node);
|
||||
else
|
||||
{
|
||||
session.delete(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user