r165 - in trunk: . nuiton-web/src/main/java/org/nuiton/web/filter
Author: tchemit Date: 2012-01-04 18:45:59 +0100 (Wed, 04 Jan 2012) New Revision: 165 Url: http://nuiton.org/repositories/revision/nuiton-web/165 Log: Anomalie #1876: When closing topia context, the transaction can stay in bad state T?\195?\162che #1877: Updates to topia 2.6.5 Modified: trunk/nuiton-web/src/main/java/org/nuiton/web/filter/TopiaTransactionFilter.java trunk/pom.xml Modified: trunk/nuiton-web/src/main/java/org/nuiton/web/filter/TopiaTransactionFilter.java =================================================================== --- trunk/nuiton-web/src/main/java/org/nuiton/web/filter/TopiaTransactionFilter.java 2012-01-04 17:19:13 UTC (rev 164) +++ trunk/nuiton-web/src/main/java/org/nuiton/web/filter/TopiaTransactionFilter.java 2012-01-04 17:45:59 UTC (rev 165) @@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.hibernate.Transaction; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaRuntimeException; @@ -186,14 +187,20 @@ TopiaContext proxy = (TopiaContext) Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[]{TopiaContext.class, - TopiaContextImplementor.class}, + TopiaContextImplementor.class}, proxyInvocationHandler ); + + // push it in request as an attribute request.setAttribute(TOPIA_TRANSACTION_REQUEST_ATTRIBUTE, proxy); try { + + // chain to next filter chain.doFilter(request, response); } finally { - proxyInvocationHandler.closeTransaction(); + + // close the real transaction + onCloseTransaction(proxyInvocationHandler.transaction); } } @@ -214,10 +221,49 @@ // not authorized throw new IllegalAccessException( "Not allowed to access method " + method.getName() + " on " + - proxy); + proxy); } /** + * Hook method to close the topia transaction of the request at the end of + * the request when all filter has been consumed. + * + * @param transaction the transaction to close (can be null if transaction + * was not required while the current request) + * @since 1.9.1 + */ + protected void onCloseTransaction(TopiaContext transaction) { + if (transaction == null) { + if (log.isTraceEnabled()) { + log.trace("no transaction to close"); + } + } else if (transaction.isClosed()) { + if (log.isTraceEnabled()) { + log.trace("transaction " + transaction + " is already closed"); + } + } else { + if (log.isDebugEnabled()) { + log.debug("closing transaction " + transaction); + } + try { + // let's rollback transaction if the transaction was not rollbacked nor committed + // as the topia context close context does not affect hibernate transaction + // so if something bad happen then we will always have a + Transaction tx = ((TopiaContextImplementor) transaction).getHibernate().getTransaction(); + if (!tx.wasCommitted() && !tx.wasRolledBack()) { + if (log.isDebugEnabled()) { + log.debug("rollback transaction!"); + } + tx.rollback(); + } + transaction.closeContext(); + } catch (TopiaException e) { + throw new TopiaRuntimeException(e); + } + } + } + + /** * Hook method called when a method with his name in * {@link #unusedMethods} was invoked on the proxied transaction * while the underlying transaction is still not opened. @@ -237,7 +283,7 @@ String methodName = method.getName(); if (log.isDebugEnabled()) { log.debug("Skip execution of method " + methodName + - " since no transaction is instanciated."); + " since no transaction is instanciated."); } Set<String> methods = getUnusedMethods(); @@ -323,27 +369,6 @@ throw eee; } } - - protected void closeTransaction() { - if (transaction == null) { - if (log.isTraceEnabled()) { - log.trace("no transaction to close"); - } - } else if (transaction.isClosed()) { - if (log.isTraceEnabled()) { - log.trace("transaction " + transaction + " is already closed"); - } - } else { - if (log.isDebugEnabled()) { - log.debug("closing transaction " + transaction); - } - try { - transaction.closeContext(); - } catch (TopiaException e) { - throw new TopiaRuntimeException(e); - } - } - } } } Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2012-01-04 17:19:13 UTC (rev 164) +++ trunk/pom.xml 2012-01-04 17:45:59 UTC (rev 165) @@ -241,7 +241,7 @@ <nuitonI18nVersion>2.4.1</nuitonI18nVersion> <nuitonI18nPluginVersion>${nuitonI18nVersion}</nuitonI18nPluginVersion> - <topiaVersion>2.6.4</topiaVersion> + <topiaVersion>2.6.5</topiaVersion> <!-- Tapestry --> <tapestryVersion>5.2.4</tapestryVersion>
participants (1)
-
tchemit@users.nuiton.org