0
我在我的應用程序中使用OpenSessionInView。 我的目標是在發生任何異常的情況下,在一種方法中回滾數據庫中的所有更改。這裏是我的控制器:交易不回滾
@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);
}
與它下面給出的配置工作:在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>
但這種策略無法保存/鑑於更新任何行。所以我ovverrided的OpenSessionInViewFilter這樣的:
public class CustomOpenSessionInViewFilter extends OpenSessionInViewFilter{
@Override
public void closeSession(Session session, SessionFactory sessionFactory){
session.flush();
super.closeSession(session,sessionFactory);
}
}
現在我可以保存/更新,但交易犯規回滾......如何使他們的工作兩者兼而有之?
您更新視圖中的數據或只是渲染它?這只是爲了我的澄清... – Pokuri