OpenEntityManagerInViewFilter not working - Spring MVC

rhinds

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?

Biju Kunjummen

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

logout not working in spring mvc

SwaggerSpringMvcPlugin not working on Spring MVC

Swagger not working with Spring MVC application

autowire by name not working spring MVC

Spring AOP is not working in with MVC structure

Spring MVC RequestMapping not working on RestController

Spring MVC, Form Submission not working

POST Method not working in Spring MVC

Spring AOP with SPring MVC controllers not working

@Cachebale not working with Ehcache and spring MVC Ehcache not working with Spring Caching Annotation

spring 5 MVC Annotation Based Application Not Working

Tiles 3 with spring MVC 3 integration not working

Spring4 MVC Controller Pageable not working

set jquery date in spring mvc controller not working

TDD test for Spring MVC controller not working correctly

Spring MVC dispatcher not working. HTTP 404

Spring MVC VersionResourceResolver / ContentVersionStrategy not working correctly in JSP

Spring MVC page to page navigation not working

STS: Serving Web Content with Spring MVC not working

Spring MVC Custom Converter for JSON data not working

why success function is not working in spring mvc

Spring Mvc test webapp @Autowired annotation is not working

spring-mvc mail not working with the ZK framework

java script is not working in jsp file spring mvc

Basic Spring MVC login page functionality not working

Spring boot POST requests are not working in Spring MVC application

Lazy Initialisation with OpenEntityManagerInViewFilter?

OpenEntityManagerInViewFilter annotation config

Spring mvc multiple file upload while working on WebSphere Liberty