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. 阿里云服务器基本搭建_错误1_Permission denied (publickey)

    首先 修改这两个密码 然后重启服务器就可以了

  2. struts2 前端显示错误信息

    当我们显示错误信息的时候,会发现错误信息会以列表的形式显示,这样就不美观了,达不到我们想要的标准.所以我们可以用另外的方式输出错误信息. 例如我现在增加了两个错误信息: this.addFieldEr ...

  3. Handler和内部类的正确用法

    PS:本文摘抄自<Android高级进阶>,仅供学习使用 Android代码中涉及线程间通信的地方经常会使用Handler,典型的代码结构如下. public class HandlerA ...

  4. PermGen space OOM 记录

    前些天线上除出了个OOM问题,今天闲下来记录下: OOM的提示信息是-PermGen space,说明问题出在方法区,方法区存的是什么东西?:类的加载信息.常量.静态变量. 按照方法区的定义:类加载的 ...

  5. MyBatis 实现分页功能

    MySQL 的分页功能是基于内存的分页(即查出来所有记录,再按起始位置和页面容量取出结果). 案例:①根据用户名(支持模糊查询).用户角色 id 查询用户列表(即根据用户名称或根据用户角色 id 又或 ...

  6. memcached协议解析 及使用

    本文转载自:http://www.ccvita.com/306.html 协议memcached 的客户端使用TCP链接与服务器通讯.(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的 ...

  7. Python之模块和包的创建与使用

    一.模块的概念 在计算机的开发过程中,随着程序代码越写越多,在一个文件里代码就越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,放在不同的文件里面,这样,每个文件包含的代码就相对 ...

  8. stark组件之处理函数动态url构造(五)

    在这个组件中有内置的4个处理函数,它们都有自己对应的url,那么它们的url是怎么构造的呢? ... urlpatterns = [ re_path('list/$', self.wrapper(se ...

  9. 使用C++调用pytorch模型(Linux)

    前言 模型转换思路通常为: Pytorch -> ONNX -> TensorRT Pytorch -> ONNX -> TVM Pytorch -> 转换工具 -> ...

  10. js给对象添加属性

    obj.prototype = shuxing: shuxing['属性']=值: function Person(){}; var person = new Person(); person.nam ...