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. CREATE LANGUAGE - 定义一种新的过程语言

    SYNOPSIS CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name HANDLER call_handler [ VALIDATOR valfunctio ...

  2. AttributeError: 'dict' object has no attribute 'encode'

    首先这是一个很简单的 运行时错误: 错误分析: AttributeError:属性错误,造成这种错误的原因可能有: 你尝试访问一个不存在的属性或方法.检查一下拼写!你可以使用内建函数 dir 来列出存 ...

  3. MySQL索引的用处

    MySQL索引在MySQL数据库中,可以有效提高查询的效率,尤其是查询数据量非常大时,效果更为明显,往往能使查询速度加快成千上万倍. MySQL索引是很重要的概念,应用的范围非常广.那么,MySQL索 ...

  4. freenas 系统可能存在的bug

    1.portal 中ip端口显示有问题. 2.创建extend/target映射之后重启iscsi服务有的时候不能启动. 3.后台/usr /etc 重启系统会自动还原.

  5. php基础排序算法

    1.冒泡排序 $arr = array(12,34,57,42,165.4,73,51); function bubbling_sort($array) { $cou = count($array); ...

  6. JVM优化(上)

    02.我们为什么要对jvm做优化: 1.标准参数:-help-version 2. -X参数(非标) -Xint-Xcomp -Xint : interpreted-Xcomp: complied   ...

  7. C++ STL容器之 stack

    STL 中的 stack 是一种容器适配器,而不是一种容器. 它是容器适配器是指,只要支持一系列方法的容器(empty, size, back, push_back, pop_back),都能作为st ...

  8. windows系统查看目录树

    进入到需要查看的目录后,按住shift键,然后单击鼠标右键,选择“在此处打开命令窗口”,然后命令行窗口界面. 1.若是只查看该目录下有哪些目录,输入tree即可 2.若是显示该目录及其子目录下的所有目 ...

  9. windows窗口过程函数名词解析

    windows窗口过程函数名词解析 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) 1. LR ...

  10. MySQL prepare语句的SQL语法

    MySQL prepare语法: PREPARE statement_name FROM preparable_SQL_statement; /*定义*/ EXECUTE statement_name ...