Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

I can't tell for Spring integration, but RequestFactory expects:

  • one session per request, aka "open session in view" (so the same Hibernate cache is used throughout the request)
  • one transaction per service method

So your point about “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” is moot and wrong.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top