Monday, December 30, 2013

Open Session In View not working (locator?)

Hi there, 

I am integrating GWT 2.5.1 with Spring 3.0 backend.  Run into a problem that OSIVP should fix but it is not. 

A parent object contains a list of children objects, both are EntityProxy. On client side, I update the child proxy and my Server side API simply saves the parent.  On the server side, my updates to the child object disappears by the time server business logic is invoked.  My transaction starts and commits in business service layer. I have already used one single entity manager instance per request by using ThreadLocal.

Without using OSIVP, I did observe if I explicitly start a transaction in my request factory servlet when request comes in, and then commit the transaction in the servlet at the end, the problem is fixed. Therefore, I have to group everything under the same transaction (inside servlet, rather than in service layer), in addition to use one single entity manager pre request, is that correct?

Everyone said that the fix is to use OSIVP, however, I do not see it is fixing the problem afterwards. For whatever reason, using OSIVP does not have the same effect. 

1) In my EntityLocator class, how do I get entityManager?   Some has posted this code, this is what I used. But, does not it start a new entity manager rather than using what is attached to the thread?

private void init() {
        HttpServletRequest request = RequestFactoryServlet.getThreadLocalRequest();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
        EntityManagerFactory entityManagerFactory = context.getBean(EntityManagerFactory.class);
        entityManager = entityManagerFactory.createEntityManager();
        // then cache this entityManager in locator instance
    }

2) In my DAO class, do I need to declare my entity manager as extended like this?  That is what I did. 

@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;

3) How does Spring OpenEntityManagerInViewFilter start a transaction? I do not see it is happening in its source code, other than registering entity manager to current thread.  I think that is my problem. If somehow a transaction is started before the locator method is called, I would have been fine. 

I wish someone has a sample project illustrates all this.

Thanks,
Yan

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment