Search code examples
springhibernategwttransactionsrequestfactory

GWT RequestFactory and Spring-managed transactions


I'm working on a GWT 2.5.1 + Spring 3.2 + Hibernate 4 project. The Spring-Hibernate application is already working and has the usual Service/Dao layers. Transactions are handled by Spring through @Transactional annotations on service methods.

I'm trying to add the GWT layer on top of it using GWT RequestFactory. In particular, I don't understand how RequestFactory and Spring-managed transactions should work together.

It seems like RequestFactory performs a lot of queries (see find/getId methods of Locator class) before and after actually calling the requested service method. BTW: Should Locator rely on a Spring service bean to perform those queries? I guess so.

This means RequestFactory do things in different transactions, which is bad. I want the whole request to happen in the same transaction to take advantage of Hibernate cache (well, this is just the main reason).

I think @Transactional annotations should somehow be moved up to the GWT server layer, but they must be on a Spring bean in order to work, so...

What's the best way to integrate GWT RequestFactory and Spring-managed transactions?


Solution

  • Thomas is right. You need to use the Open Session in View pattern. Regarding Spring integration:

    Just add this to your web.xml:

    <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
        <init-param>
            <param-name>entityManagerFactoryBeanName</param-name>
            <param-value>entityManagerFactory</param-value>
        </init-param>
     </filter>
     <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>
    

    Annotate your Service classes or methods with @Transactional.