Here is our implementation with Spring 3.1.0 - it lets you use the
Spring transaction annotations on your Spring services.

First in your module:

public static void bind(ServiceBinder binder) {
        binder.bind(ComponentRequestFilter.class,
OpenSpringSessionInViewFilter.class).withSimpleId();
}

public static void
contributeComponentRequestHandler(OrderedConfiguration<ComponentRequestFilter>
configuration, OpenSpringSessionInViewFilter filter) {
        configuration.add("OpenSessionInViewFilter", filter);
}

And here's the class - it was created by collapsing the usual Spring
hierarchy of 18,001 classes and interfaces to about 8 lines of useful
code. (Shesh!)

import java.io.IOException;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ComponentEventRequestParameters;
import org.apache.tapestry5.services.ComponentRequestFilter;
import org.apache.tapestry5.services.ComponentRequestHandler;
import org.apache.tapestry5.services.PageRenderRequestParameters;
import org.apache.tapestry5.services.Request;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import 
org.springframework.transaction.support.TransactionSynchronizationManager;



public class OpenSpringSessionInViewFilter implements ComponentRequestFilter {
        private static final Logger LOG =
LoggerFactory.getLogger(OpenSpringSessionInViewFilter.class);

        @Inject
        private ApplicationContext applicationContext;

        @Inject
        private Request request;



        @Override
        public void handlePageRender(final PageRenderRequestParameters
parameters, final ComponentRequestHandler handler) throws IOException
{
                if (alreadyFiltered()) {
                        // Proceed without invoking this filter...
                        handler.handlePageRender(parameters);
                } else {
                        doFilterInternal(new PageRenderEventHandler(handler, 
parameters));
                }
        }

        @Override
        public void handleComponentEvent(final
ComponentEventRequestParameters parameters, final
ComponentRequestHandler handler) throws IOException {
                if (alreadyFiltered()) {
                        // Proceed without invoking this filter...
                        handler.handleComponentEvent(parameters);
                } else {
                        doFilterInternal(new ComponentEventHandler(handler, 
parameters));
                }
        }



        private void doFilterInternal(EventHandler stuff) throws IOException {
                SessionFactory sessionFactory =
applicationContext.getBean("sessionFactory", SessionFactory.class);
                boolean participate = false;

                // single session mode
                if 
(TransactionSynchronizationManager.hasResource(sessionFactory)) {
                        // Do not modify the Session: just set the participate 
flag.
                        participate = true;
                } else {
                        LOG.debug("Opening single Hibernate Session in 
OpenSessionInViewFilter");
                        Session session = getSession(sessionFactory);
                        
TransactionSynchronizationManager.bindResource(sessionFactory, new
SessionHolder(session));
                }

                try {
                        stuff.doStuff();

                } finally {
                        if (!participate) {
                                SessionHolder sessionHolder = (SessionHolder)
TransactionSynchronizationManager.unbindResource(sessionFactory);
                                LOG.debug("Closing single Hibernate Session in 
OpenSessionInViewFilter");
                                
SessionFactoryUtils.closeSession(sessionHolder.getSession());
                        }
                }
        }

        @SuppressWarnings("deprecation")
        private Session getSession(SessionFactory sessionFactory) {
                Session session = 
SessionFactoryUtils.getSession(sessionFactory, true);
                session.setFlushMode(FlushMode.NEVER);
                return session;
        }

        private boolean alreadyFiltered() {
                String alreadyFilteredAttributeName =
"OpenSpringSessionInViewFilter.FILTERED";
                boolean alreadyFiltered =
request.getAttribute(alreadyFilteredAttributeName) != null;
                request.setAttribute(alreadyFilteredAttributeName, 
Boolean.TRUE);
                return alreadyFiltered;
        }

        private final class PageRenderEventHandler implements EventHandler {
                private final ComponentRequestHandler handler;
                private final PageRenderRequestParameters parameters;

                private PageRenderEventHandler(ComponentRequestHandler handler,
PageRenderRequestParameters parameters) {
                        this.handler = handler;
                        this.parameters = parameters;
                }

                @Override
                public void doStuff() throws IOException {
                        handler.handlePageRender(parameters);
                }
        }

        private final class ComponentEventHandler implements EventHandler {
                private final ComponentRequestHandler handler;
                private final ComponentEventRequestParameters parameters;

                private ComponentEventHandler(ComponentRequestHandler handler,
ComponentEventRequestParameters parameters) {
                        this.handler = handler;
                        this.parameters = parameters;
                }

                @Override
                public void doStuff() throws IOException {
                        handler.handleComponentEvent(parameters);
                }
        }

        private interface EventHandler {
                void doStuff() throws IOException;
        }
}



On 23 May 2012 05:53, bhorvat <horvat.z.bo...@gmail.com> wrote:
> You are right I wont pollute this mail thread with that anymore, I will try
> to do a bit more research on one vs the other.
>
> But in the mean time if anyone has any suggestion and nice links about
> spring-transactions and OSIVF please share it will be of great help
>
> cheers
>
> --
> View this message in context: 
> http://tapestry.1045711.n5.nabble.com/Tapestry-Transactions-tp5713299p5713343.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to