step1:配置xml文件

 <!-- 事务管理bean -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
</bean> <!-- 对@Transactional这个注解进行的驱动,这是基于注解的方式使用事务配置声明 -->
<tx:annotation-driven transaction-manager="transactionManager" />

step2:自动注入TransactionTemplate对象

 import org.springframework.transaction.support.TransactionTemplate;
@Autowired
private TransactionTemplate transactionTemplate;

step3:使用execute方法对事务进行管理

 transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                 @Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
String s = userService.newUser(user,currentUserId);
User newUser=userService.getUser(user.getUsername());
roleService.createRoleUser(newUser.getUserId(), rolesId);
}
});

完整流程如下:

 package com.superb.mips.terminal.rest.controller;

 import java.util.HashMap;

 import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.superb.mips.system.domain.User;
import com.superb.mips.system.service.inf.RoleService;
import com.superb.mips.system.service.inf.UserService; @Controller
@RequestMapping("api/users")
public class RestUserController {
@Autowired
private UserService userService;
&orgName=中国移动&orgId=1
@Autowired
private RoleService roleService;
@Autowired
private TransactionTemplate transactionTemplate; @RequestMapping(value = "/createUsers.json")
@ResponseBody
@Transactional
public HashMap<String, Object> createUser(final User user,final int currentUserId,final String rolesId) {
HashMap<String, Object> map = new HashMap<String, Object>();
String[] strAry=rolesId.split(",");
if(strAry.length==0){
map.put("result", 1);
map.put("description", "请绑定角色");
return map;
}else{
for(int i=0;i<strAry.length;i++){
try {
Integer.parseInt(strAry[i]);
} catch (Exception e) {
map.put("result", 2);
map.put("description", "请绑定正确的角色id");
return map;
}
}
}
try {
transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
String s = userService.newUser(user,currentUserId);
User newUser=userService.getUser(user.getUsername());
roleService.createRoleUser(newUser.getUserId(), rolesId);
}
});
map.put("result", 0);
map.put("description", "新增用户成功。");
} catch (Exception e) {
map.put("result", 3);
map.put("description", "新增用户失败!");
} return map;
}
}

spring中编程式事务控制的更多相关文章

  1. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  2. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  3. Spring的编程式事务和声明式事务

    事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务管理的两种方式 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务使用Transa ...

  4. Spring笔记(4) - Spring的编程式事务和声明式事务详解

    一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...

  5. 【Spring】编程式事务和声明式事务

    一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...

  6. 阶段3 2.Spring_10.Spring中事务控制_9 spring编程式事务控制1-了解

    编程式的事物控制,使用的情况非常少,主要作为了解 新建项目 首先导入包坐标 复制代码 这里默认值配置了Service.dao和连接池其他的内容都没有配置 也就说现在是没有事物支持的.运行测试文件 有错 ...

  7. 阶段3 2.Spring_10.Spring中事务控制_10spring编程式事务控制2-了解

    在业务层声明 transactionTemplate 并且声称一个set方法等着spring来注入 在需要事物控制的地方执行 execute.但是这个execute需要一个参数 需要的参数是Trans ...

  8. (转)Spring的编程式事务例子

    纯JDBC操作, 对某些项目来说, 也许更好, Spring JDBC Framework让你不用关心Connection, Statement, ResultSet. 定义数据源 spring事务编 ...

  9. spring 采用编程式事务

    1.getCurrentSession()与openSession()的区别? * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession() ...

随机推荐

  1. 第4节 hive调优:1、2、fetch抓取和表的优化

    hive的调优:第一个调优:fetch抓取,能够避免使用mr的,就尽量不要用mr,因为mr太慢了 set hive.fetch.task.conversion=more 表示我们的全局查找,字段查找, ...

  2. [转载]在网页中插入media,RealPlayer等控件

    [转载]在网页中插入media,RealPlayer等控件 (2012-11-02 20:27:43) 转载▼ 标签: 转载   原文地址:在网页中插入media,RealPlayer等控件作者:Mo ...

  3. LNMP环境搭建网站

    系统:Centos7.4 一台服务器安装nginx.php.mysql 原理不是很懂,但实测如下两种环境下都能成功,这里做下记录: 1.一个干净的系统,从0开始完成所有步骤,完成LNMP搭建 2.已经 ...

  4. [Python3网络爬虫开发实战] 1.3.3-pyquery的安装

    pyquery同样是一个强大的网页解析工具,它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便.本节中,我们就来了解一下它的安装方式. 1. 相关链接 GitHub:h ...

  5. js中给正则传参、传递变量

    js中验证字符串有时需要用到正则表达式,一般情况下直接写正则进行验证就行. 但是遇到需要把部分正则作为参数传递就麻烦一点,需要用到RegExp()对象. <script type="t ...

  6. Poj 2187 凸包模板求解

    Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...

  7. 第二周习题F

    Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2  ...

  8. <vue>…… v-if 与 v-show ……//

    #v-if 用法: 根据表达式的值的真假条件渲染元素.在切换时元素及它的数据绑定 / 组件被销毁并重建.如果元素是 <template> ,将提出它的内容作为条件块. 当条件变化时该指令触 ...

  9. 九度oj 题目1023:EXCEL排序

    题目1023:EXCEL排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20699 解决:4649 题目描述:     Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似 ...

  10. uva1366/LA3530

    简单的矩阵上的dp 从左上角扫到右下角,dp[i][j][0] 代表在i,j处选A ,dp[i][j][1] 代表在i,j处选B. dp[i][j][0]=max(dp[i-1][j][1],dp[i ...