关于spring在controller层的事务操作
来源:互联网 发布:印度it为何这么强 知乎 编辑:程序博客网 时间:2024/06/05 19:02
以下是代码:
package cn.hr.controller;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import cn.hr.base.action.BaseAction;import cn.hr.service.IAnnualbonuService;@Controller@RequestMapping(value="/demoTest")public class DemoTestController extends BaseAction<Object>{ @Autowired private IAnnualbonuService annualbonuService; @Autowired private PlatformTransactionManager transactionManager; /** * * @param payNamelist * @param request * @param response * @param session */ @RequestMapping(value = "/deletePayNamelist",method = RequestMethod.POST) public void deletePayNamelist(HttpServletRequest request, HttpServletResponse response,HttpSession session) { PrintWriter out = null; TransactionStatus status = this.transaction(); try { //=====================业务逻辑处理地方================================ out=response.getWriter(); out.write("0"); out.flush(); transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); out.write(""); out.flush(); logger.error(e); }finally{ out.flush(); out.close(); } } private TransactionStatus transaction(){ DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition); return status; }}
主要是transaction这个方法,意思是:new 一个新的事务,再设置自己所需要的事务隔离级别,最后通过注入的transactionManager得到该事务即可。百分百生效!
阅读全文
0 0
- 关于spring在controller层的事务操作
- spring在controller层声明事务
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- Spring MVC中,事务是否可以加在Controller层
- 在Spring中配置Service层方法的事务
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- 关于Spring事务<tx:annotation-driven/>的理解(Controller可以使用@Transactional)
- spring开启事务在service层
- 关于如何controller层的构造
- spring引入properties文件,并在controller等层获得引入文件中属性的值
- spring引入properties文件,并在controller等层获得引入文件中属性的值
- 1 Spring MVC View层与Controller层的衔接
- 有兴趣的进来
- 将微信公众号的文章保存到本地pdf
- 阿里云创建E-MapReduce 1 快速开始
- Android espresso-web等待网页元素方法
- cat /proc/net/dev 查询网口状况
- 关于spring在controller层的事务操作
- python中的join和split
- hud 1874畅通工程续(6种写法,复习各种最短路)
- 再搞SQLServer添加Oracle链接服务器
- C#Winform调用libEasyPlayer总结
- Linux CentOS 6.6安装JDK1.7
- eclipse导入项目,项目名出现红叉的情况
- Windows版MYSQL5.7安装(压缩包版)
- 自动取消失效订单(spring整合quartz)