1. 程式人生 > >spring中編程式事務控制

spring中編程式事務控制

intra ans res ren nbsp new temp class 進行

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中編程式事務控制