spring中编程式事务控制

Posted 根须

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring中编程式事务控制相关的知识,希望对你有一定的参考价值。

step1:配置xml文件

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

step2:自动注入TransactionTemplate对象

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

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

1 transactionTemplate.execute(new TransactionCallbackWithoutResult() {
2                 
3                 @Override
4                 protected void doInTransactionWithoutResult(TransactionStatus arg0) {
5                     String s = userService.newUser(user,currentUserId);
6                     User newUser=userService.getUser(user.getUsername());
7                     roleService.createRoleUser(newUser.getUserId(), rolesId);
8                 }
9             });

完整流程如下:

 1 package com.superb.mips.terminal.rest.controller;
 2 
 3 import java.util.HashMap;
 4 
 5 import org.apache.commons.lang3.StringUtils;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.transaction.TransactionStatus;
 9 import org.springframework.transaction.annotation.Transactional;
10 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
11 import org.springframework.transaction.support.TransactionTemplate;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.ResponseBody;
14 
15 import com.superb.mips.system.domain.User;
16 import com.superb.mips.system.service.inf.RoleService;
17 import com.superb.mips.system.service.inf.UserService;
18 
19 
20 @Controller
21 @RequestMapping("api/users")
22 public class RestUserController {
23     @Autowired
24     private UserService userService;
25     &orgName=中国移动&orgId=1
26     @Autowired
27     private RoleService roleService;
28     @Autowired
29     private TransactionTemplate transactionTemplate;
30 
31     @RequestMapping(value = "/createUsers.json")
32     @ResponseBody
33     @Transactional
34     public HashMap<String, Object> createUser(final User user,final int currentUserId,final String rolesId) {
35         HashMap<String, Object> map = new HashMap<String, Object>();
36         String[] strAry=rolesId.split(",");
37         if(strAry.length==0){
38             map.put("result", 1);
39             map.put("description", "请绑定角色");
40             return map;
41         }else{
42             for(int i=0;i<strAry.length;i++){
43                 try {
44                     Integer.parseInt(strAry[i]);
45                 } catch (Exception e) {
46                     map.put("result", 2);
47                     map.put("description", "请绑定正确的角色id");
48                     return map;
49                 }
50             }
51         }
52         try {
53             transactionTemplate.execute(new TransactionCallbackWithoutResult() {
54                 
55                 @Override
56                 protected void doInTransactionWithoutResult(TransactionStatus arg0) {
57                     String s = userService.newUser(user,currentUserId);
58                     User newUser=userService.getUser(user.getUsername());
59                     roleService.createRoleUser(newUser.getUserId(), rolesId);
60                 }
61             });
62             map.put("result", 0);
63             map.put("description", "新增用户成功。");
64         } catch (Exception e) {
65             map.put("result", 3);
66             map.put("description", "新增用户失败!");
67         }
68         
69         return map;
70     }
71 }

 

以上是关于spring中编程式事务控制的主要内容,如果未能解决你的问题,请参考以下文章

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

Spring 事务控制 -- 编程式事务控制相关对象

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

spring来了-06-事务控制

spring学习笔记声明式事务

spring 事务控制