spring中编程式事务控制
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中编程式事务控制的更多相关文章
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring的编程式事务和声明式事务
事务管理对于企业应用来说是至关重要的,当出现异常情况时,它也可以保证数据的一致性. Spring事务管理的两种方式 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务使用Transa ...
- Spring笔记(4) - Spring的编程式事务和声明式事务详解
一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...
- 【Spring】编程式事务和声明式事务
一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...
- 阶段3 2.Spring_10.Spring中事务控制_9 spring编程式事务控制1-了解
编程式的事物控制,使用的情况非常少,主要作为了解 新建项目 首先导入包坐标 复制代码 这里默认值配置了Service.dao和连接池其他的内容都没有配置 也就说现在是没有事物支持的.运行测试文件 有错 ...
- 阶段3 2.Spring_10.Spring中事务控制_10spring编程式事务控制2-了解
在业务层声明 transactionTemplate 并且声称一个set方法等着spring来注入 在需要事物控制的地方执行 execute.但是这个execute需要一个参数 需要的参数是Trans ...
- (转)Spring的编程式事务例子
纯JDBC操作, 对某些项目来说, 也许更好, Spring JDBC Framework让你不用关心Connection, Statement, ResultSet. 定义数据源 spring事务编 ...
- spring 采用编程式事务
1.getCurrentSession()与openSession()的区别? * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession() ...
随机推荐
- 三个层面学playbook(核心)
三个层面学playbook(核心) ansible-playbook是ansible工具中的核心,对比ad-hoc(ansible)命令,可以把playbook理解为一系列动作的组成,结果传递.判断等 ...
- vim跳转(一)
参考资料:http://easwy.com/blog/archives/advanced-vim-skills-basic-move-method/ 在normal模式下使用如下命令 1.h, j, ...
- [C#] 对List进行分组排序后输出
Student 类: public class Student { public int ID { get; set; } public string Name { get; set; } publi ...
- HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"&g ...
- EZOJ 宝石迷阵 建图+网络流匹配
题面: 封印恶魔的地方可以看作是一个 n*m 的矩形,包含了 n*m 个祭坛,并且其 中有一些祭坛已经损坏了. 如果 i+j 为偶数,那么第 i 行第 j 列的祭坛只要没有损坏,就一定会封印有一个恶魔 ...
- 简谈Redis
1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中 ...
- Git--使用须知123
详细的篇幅以后补充 安装篇: 设置篇: 由于我们大多数是windows程序员,那么,在使用git的过程前需要做一些设置项. 1.换行符自动转换. 查看:git config --global --li ...
- POJ 3264 RMQ问题 用dp解决
#include <cstdio> #include <cstring> #include <iostream> using namespace std; ; #d ...
- ZOJ2193 AOV建模
每个窗口有四个小区域组成,那么不断往前递推,到达打开当前窗口时必然是那些在上面出现的窗口都已经被打开过了,那么我们可以认为是在第i个窗口的位置上出现了 j , 那么in[i]++ , 只有 i 入度为 ...
- 使用Spring-Session共享使用Session
前言: session共享策略有很多,常见的有粘性复制,高并发下效率查.tomcat-redis-session-manager无疑是一个挺好的方案,缺点要配置tomcat,有点复杂.最优的方案莫过于 ...