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:
With configuration given below it works: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); }
in web.xml:
in hbm-config.xml:Code:<filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter>
But in this strategy I cannot save/update any row in view. So I ovverrided OpenSessionInViewFilter like this:Code:<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
Now I can save/update, but Transaction doesnt rollback... how to make them work both?Code:public class CustomOpenSessionInViewFilter extends OpenSessionInViewFilter{ @Override public void closeSession(Session session, SessionFactory sessionFactory){ session.flush(); super.closeSession(session,sessionFactory); } }


Reply With Quote