From: <id...@us...> - 2008-10-22 10:22:49
|
Revision: 4832 http://openuss.svn.sourceforge.net/openuss/?rev=4832&view=rev Author: idueppe Date: 2008-10-22 09:24:51 +0000 (Wed, 22 Oct 2008) Log Message: ----------- Add a simple implementation of a method interceptor to handle concurrent update exceptions by trying the operation again Added Paths: ----------- branches/openuss-plexus-3.1/framework/core/src/main/java/org/openuss/framework/transaction/ branches/openuss-plexus-3.1/framework/core/src/main/java/org/openuss/framework/transaction/ConcurrentUpdateInterceptor.java Added: branches/openuss-plexus-3.1/framework/core/src/main/java/org/openuss/framework/transaction/ConcurrentUpdateInterceptor.java =================================================================== --- branches/openuss-plexus-3.1/framework/core/src/main/java/org/openuss/framework/transaction/ConcurrentUpdateInterceptor.java (rev 0) +++ branches/openuss-plexus-3.1/framework/core/src/main/java/org/openuss/framework/transaction/ConcurrentUpdateInterceptor.java 2008-10-22 09:24:51 UTC (rev 4832) @@ -0,0 +1,54 @@ +package org.openuss.framework.transaction; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.hibernate.FlushMode; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.jdbc.UncategorizedSQLException; + +public class ConcurrentUpdateInterceptor implements MethodInterceptor { + + private final Exception UNKNOWN = new Exception("Unknown!"); + + private SessionFactory sessionFactory; + + private int maxTries; + + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + int tries = 0; + Throwable exception = UNKNOWN; + while (tries++ < maxTries) { + try { + return invocation.proceed(); + } catch (Throwable ex) { + if (ex instanceof UncategorizedSQLException && tries < maxTries) { + Session session = sessionFactory.getCurrentSession(); + session.setFlushMode(FlushMode.AUTO); + exception = ex; + } else { + throw ex; + } + } + } + throw exception; + } + + public int getMaxTries() { + return maxTries; + } + + public void setMaxTries(int maxTries) { + this.maxTries = maxTries; + } + + public SessionFactory getSessionFactory() { + return sessionFactory; + } + + public void setSessionFactory(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + +} Property changes on: branches/openuss-plexus-3.1/framework/core/src/main/java/org/openuss/framework/transaction/ConcurrentUpdateInterceptor.java ___________________________________________________________________ Added: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |