Results 1 to 2 of 2

Thread: Transaction does not rollback In my OpenSessionInViewFilter

  1. #1
    Join Date
    Dec 2010
    Posts
    11

    Default Transaction does not rollback In my OpenSessionInViewFilter

    I use OpenSessionInView in my application.
    My goal is to rollback all change in database in one method if any exception occurs. Here is my controller:

    Code:
    @RequestMapping(value="/kartazadan.do", method=RequestMethod.GET )
    	@Transactional(rollbackFor=Exception.class)
    	public ModelAndView viewGET(HttpServletRequest request,
    			HttpServletResponse response) throws Exception{
    		int id = Integer.parseInt(ServletRequestUtils.getRequiredStringParameter(request, "id")); 
    		ModelMap modelMap = new ModelMap();
    		KartaZadan kartaZadan = kartaZadanDAO.getkartaZadanById(id);
    		kartaZadan.setZadanie("TEST10");
    		kartaZadanDAO.update(kartaZadan);
    		kartaZadan = kartaZadanDAO.getkartaZadanById(null); //here when Exception should occurr and make transaction rollback
    		kartaZadanDAO.update(kartaZadan);
    		modelMap.addAttribute("kartaZadan", kartaZadan);
    		setCommonFields(modelMap);
    		modelMap.addAttribute("errorsEnabled", false);
    		return new ModelAndView("kartaZadan", modelMap);
    	}
    With configuration given below it works:
    in web.xml:
    Code:
    	<filter>
            <filter-name>hibernateFilter</filter-name>
            <filter-class>
               org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
            </filter-class>
    </filter>
    in hbm-config.xml:
    Code:
    <bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> 
    	    <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    But in this strategy I cannot save/update any row in view. So I ovverrided OpenSessionInViewFilter like this:
    Code:
    public class CustomOpenSessionInViewFilter extends OpenSessionInViewFilter{
    	
    	@Override
    	public void closeSession(Session session, SessionFactory sessionFactory){
    		session.flush();
    		super.closeSession(session,sessionFactory);
    	}
    
    }
    Now I can save/update, but Transaction doesnt rollback... how to make them work both?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    OpenSessionInView has NOTHING to do with transactions. If you transactions aren't working your transaction setup is wrong. Also you should need either the OpenSessionInViewInterceptor or the Filter NOT BOTH.

    Also your controller contains business logic this shouldn't be the case a controller should be thin, it should be merely a integration layer for the web to your service layer. However to solve your problem fix your transactions... Judging from the snippets here you either don't have tx:annotation-driven or have tx:annotation-driven in the ContextLoaderListener but not in the DispatcherServlet.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •