0

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:

@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:

<filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>
           org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
</filter>

<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:

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?

Cichy
  • 1,319
  • 3
  • 20
  • 36

1 Answers1

0

What you are facing might be because autoCommit is turned on . You will have to turn this off

<property name="hibernate.connection.autocommit">false</property> 

and explicitly do the commit yourself via session.getTransaction().commit if everything is fine.

Aravind A
  • 9,507
  • 4
  • 36
  • 45