0

I am using OpenEntityManagerInView in my application, so that EntityManager is open and closed automatically as per the request lifecycle. Also, I have one filter in my application that is responsible for providing authentication via interacting with DB.

@Component
public class AuthFilter extends OncePerRequestFilter {
    @Autowired
    IAuthService service;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws IOException {
        /*doing auth stuff here: 
         *service.validateUser(payload);
         *responsible for opening and closing em
        */
        filterChain.doFilter(servletRequest, response);
    }
}


Since this filter is talking with DB, it's opening EntityManager during its operation and closing after it is done(automatically due to @Transactional annotation).

After successful authentication, it's calling other filters if any in the chain.

The challenge which I am facing is OpenEntityManagerInViewInterceptor is called after the above filter is done with its authentication, which in turn is leading to the session being open and closed two times per request, first on auth filter and second on OpenEntityManagerInView

How can I make OpenEntityManagerInViewInterceptor being called before all the filters specified in the application, so that filter can use already open EntityManager for DB operations(if req)

Tried @Order(Ordered.LOWEST_PRECEDENCE) above filter, but didn't worked

Faramarz Afzali
  • 726
  • 1
  • 10
  • 24
abhi_awake
  • 186
  • 1
  • 8

2 Answers2

0

You can only configure the order of servlet filters through a web.xml

Also see https://stackoverflow.com/a/6561816/412446

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
0

Try using a HandlerInterceptor instead of a servlet filter and set it's order to be after the OpenEntityManagerInViewInterceptor. See https://stackoverflow.com/a/76210504/98632

mtpettyp
  • 5,533
  • 1
  • 33
  • 33