Announcement Announcement Module
Collapse
No announcement yet.
Transaction does not rollback In my OpenSessionInViewFilter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X