1

I'm developing a web app with Spring 4.1.7 and Hibernate 4.3.10. I needed to create an Interceptor for manage transactions like this:

public class ControllerInterceptor extends HandlerInterceptorAdapter
{

@Autowired
private SessionFactory sessionFactory;

private Session session;

@Override
public boolean preHandle(HttpServletRequest request,
                         HttpServletResponse response,
                         Object handler) throws Exception
{
    super.preHandle(request, response, handler);
    ControllerInterceptor ci = this;
    session = sessionFactory.getCurrentSession();
    //Transaction management....
    return true;
}

@Override
public void afterCompletion(HttpServletRequest request,
                            HttpServletResponse response,
                            Object handler,
                            Exception ex) throws Exception
{
    //Transaction commit/rollback
    if (null != session && session.isOpen())
    {
        try
        {
            session.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
}

In applicationContext.xml I defined the interceptor in this way:

<mvc:interceptors>
    <bean class="com.interceptor.ControllerInterceptor" />
</mvc:interceptors> 

My ControllerInterceptor is singleton but I need it in request scope. I tried to define the interceptor in this way:

<mvc:interceptors>
    <bean class="com.interceptor.ControllerInterceptor" scope="request" />
</mvc:interceptors> 

but I had this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

Any suggestion?Thanks

user3363936
  • 15
  • 1
  • 6

1 Answers1

-1

MVC interceptors need not have request scope as they are executed for current request only.

To quote from java docs of HandlerInterceptor.preHandle -

Intercept the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler. DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending a HTTP error or writing a custom response.

Parameters:
request - current HTTP request
response - current HTTP response
handler - chosen handler to execute, for type and/or instance evaluation

Moreover as pointed correctly by Olesksii you need to avoid the anti-pattern of session / transaction management here.

Instead have a look at @Transactional. Suggested reads -> this , this & this

Community
  • 1
  • 1
Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59