rhinds
rhinds

Reputation: 10043

OpenEntityManagerInViewFilter not working - Spring MVC

Ok, so I am once again going nuts trying to solve the OpenEntityManagerInViewFilter problem.

I have looked around a bunch, read a lot of the other questions (that this might seem like a duplicate of) but no joy so far.

So here's the deal: Spring4, XML based web.xml but java vconfig for the rest of my app context setup. I thought it might be related to this solution: https://stackoverflow.com/a/7015927/258813 (the ContextLoadListener and the servlet config both loading the app contexts), however, I have ensured that they both explicitly reference different configuration files. I have also previously had problems when different config files were @ComponentScan-ing the same locations so the context was loaded twice, but that is not the case either.

Web.xml (relevant bits)

<servlet>
        <servlet-name>webapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.tmm.frm.configuration.WebMvcConfiguration</param-value>
        </init-param>
    </servlet>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        com.tmm.frm.configuration.ApiSecurityConfig
        com.tmm.frm.configuration.ApplicationContextConfiguration
        com.tmm.frm.configuration.WebSecurityConfig
    </param-value>
</context-param>

The security configs are both just standard WebSecurityConfigurerAdapter extensions, no other scanning/context jazz.

WebMvc config:

@Configuration
@EnableWebMvc
@ComponentScan("com.tmm.frm.controller")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter{

App Context Config (persistence stuff etc)

@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
@ComponentScan({"com.tmm.frm.service", "com.tmm.frm.helper","com.tmm.frm.core.dao", "com.tmm.frm.security"})
@PropertySource("classpath:META-INF/spring/database.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ApplicationContextConfiguration {

So both the relevant config classes are specifically defining different packages etc. Web.xml names configs to load by name so neither config classes are being loaded twice.

Then, I hit the controller - I load the user profile from the Secuirty Context (in the controller), then I jsut try to loop through a collection on the UserProfile that is lazily loaded - I would expect the OpenEntityManagerInViewFilter to kick in and load the collection (as a session is still open) but I get the normal can't load lazy object error. The logs clearly states that the filter is called, so I assume somewhere there are two contexts - any one suggest where the rogue context might be?

Upvotes: 0

Views: 956

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

From what you have said, you are loading the UserProfile from the security context, presumably you have loaded up the security context at user login. Now, the EntityManagerthat you used to load up the UserProfile at login to load up the SecurityContext is definitely not going to be valid for a new request coming into system past the login and hence the error. The scope is tied to one web request scope.

The only good workaround that I would see is to only keep some identifier of the user in the securitycontext and load up the actual user details when you need additional details or store initially with additional details.

Upvotes: 1

Related Questions