spring 事務 demo
阿新 • • 發佈:2018-11-22
1 import com.demo.service.IUserService; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.springframework.test.context.ContextConfiguration; 5 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 6 7 import javax.annotation.Resource;TestUserService8 9 10 11 @RunWith(SpringJUnit4ClassRunner.class) 12 @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) 13 public class TestUserService { 14 15 @Resource 16 IUserService userService; 17 18 /**************************Nest事務 ****************************/ 19/** 20 * 無異常 21 * @throws Exception 22 */ 23 @Test 24 public void testFun1() throws Exception { 25 userService.fun1(); 26 } 27 /** 28 * 內部事務 有異常 29 * @throws Exception 30 */ 31 @Test 32 public void testFun2() throws Exception {33 try { 34 userService.fun2(); 35 }catch(Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 40 /** 41 * 內部事務 有異常 try catch 42 * @throws Exception 43 */ 44 @Test 45 public void testFun2_2() throws Exception { 46 userService.fun2_2(); 47 } 48 49 /** 50 * 外部事務 異常 51 * @throws Exception 52 */ 53 @Test 54 public void testFun3() throws Exception { 55 userService.fun3(); 56 } 57 58 /***********require_new ***************************************************/ 59 /** 60 * 無異常 61 * @throws Exception 62 */ 63 @Test 64 public void testFun4() throws Exception { 65 userService.fun4(); 66 } 67 /** 68 * 內部事務異常 69 * @throws Exception 70 */ 71 @Test 72 public void testFun4_2() throws Exception { 73 userService.fun4_2(); 74 } 75 /** 76 * 內部事務異常 try catch 77 * @throws Exception 78 */ 79 @Test 80 public void testFun4_3() throws Exception { 81 userService.fun4_3(); 82 } 83 84 /** 85 * 外部事務異常 86 * @throws Exception 87 */ 88 @Test 89 public void testFun5() throws Exception { 90 userService.fun5(); 91 } 92 93 /*****************required*****************************/ 94 /** 95 * 內部事務異常 96 * @throws Exception 97 */ 98 @Test 99 public void testFun6() throws Exception { 100 userService.fun6(); 101 } 102 /** 103 * 內部事務異常 try catch 104 * @throws Exception 105 */ 106 @Test 107 public void testFun6_2() throws Exception { 108 userService.fun6_2(); 109 } 110 111 112 113 }
1 package com.demo.service.impl; 2 3 import com.demo.UserEntity; 4 import com.demo.dao.IUserDAO; 5 import com.demo.service.IUserService; 6 import com.demo.service.IUserService2; 7 import org.springframework.aop.framework.AopContext; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Propagation; 10 import org.springframework.transaction.annotation.Transactional; 11 12 import javax.annotation.Resource; 13 14 15 @Service 16 public class IUserServiceImpl implements IUserService { 17 18 @Resource 19 IUserDAO userDAO; 20 21 @Resource 22 IUserService2 userService2; 23 /** 24 * fun1()預設PROPAGATION_REQUIRED 25 * funNest() PROPAGATION_NESTED 無異�? 26 * 27 */ 28 @Override 29 @Transactional 30 public void fun1() throws Exception { 31 32 //資料庫操�? 33 funNone(); 34 //呼叫另一個service的方�? 35 userService2.funNest(); 36 } 37 38 /** 39 * fun2()預設PROPAGATION_REQUIRED 40 * funNestException() PROPAGATION_NESTED 異常 41 * 42 */ 43 @Override 44 @Transactional 45 public void fun2() throws Exception { 46 //巢狀事務的使用場�? 47 funNone(); 48 //當所呼叫的方法為NESTED事務,該事務的回滾可以不影響到呼叫者的事務 49 //當然如果沒有catch exception,異常冒泡而出,就將觸發呼叫者事務的回滾 50 userService2.funNestException(); 51 // userService2.funRequire(); 52 53 54 } 55 56 /** 57 * fun2_2()預設PROPAGATION_REQUIRED 58 * funNestException() PROPAGATION_NESTED 異常 59 * 60 */ 61 @Override 62 @Transactional 63 public void fun2_2() throws Exception { 64 //巢狀事務的使用場�? 65 funNone(); 66 67 try { 68 //當所呼叫的方法為NESTED事務,該事務的回滾可以不影響到呼叫者的事務 69 //當然如果沒有catch exception,異常冒泡而出,就將觸發呼叫者事務的回滾 70 userService2.funNestException(); 71 } catch (Exception e) { 72 //do something 73 } 74 75 userService2.funRequire(); 76 77 78 } 79 80 /** 81 * fun2_2()預設PROPAGATION_REQUIRED 82 * funNestException() PROPAGATION_NESTED 83 * 外部事務異常 84 */ 85 @Override 86 @Transactional 87 public void fun3() throws Exception { 88 89 funNone(); 90 //呼叫的事務為NESTED事務的方�? 91 userService2.funNest(); 92 93 //此時在呼叫�?�處,觸發�?個unchecked異常 94 throwExcp(); 95 96 97 //此時會發現包括呼叫的userService2.funNest()也被回滾�? 98 //也就是說,當呼叫的方法是NESTED事務,該方法丟擲異常如果得到了處理(try-catch),那麼該方法發生異常不會觸發整個方法的回滾 99 //而呼叫�?�出現unchecked異常,卻能觸發�?呼叫的nested事務的回�?. 100 } 101 102 @Override 103 @Transactional 104 public void fun4() throws Exception { 105 106 //資料庫操�? 107 funNone(); 108 //呼叫RequireNew型別事務的方�?,呼叫者的異常回滾不會影響到它 109 userService2.funRequireNew(); 110 //資料庫操�? 111 funNone(); 112 } 113 114 @Override 115 @Transactional 116 public void fun4_2() throws Exception { 117 //而REQUIRES_NEW,當被呼叫�?,就相當於暫停(掛起)當前事務, 118 //先開�?個新的事務去執行REQUIRES_NEW的方�?,如果REQUIRES_NEW異常 119 funNone(); 120 121 userService2.funRequireNewException(); 122 123 } 124 125 @Override 126 @Transactional 127 public void fun4_3() throws Exception { 128 //而REQUIRES_NEW,當被呼叫�?,就相當於暫停(掛起)當前事務,先開�?個新的事務去執行REQUIRES_NEW的方�?,如果REQUIRES_NEW中的異常得到了處�? 129 //那麼他將不影響呼叫�?�的事務,同時,呼叫者之後出現了異常,同樣也不會影響之前呼叫的REQUIRES_NEW方法的事�?. 130 131 //不會回滾 132 funNone(); 133 try { 134 //當異常得到處�? 135 userService2.funRequireNewException(); 136 } catch (Exception e) { 137 138 } 139 } 140 141 @Override 142 @Transactional 143 public void fun5() throws Exception { 144 145 //資料庫操�? 146 funNone(); 147 //呼叫RequireNew型別事務的方�?,呼叫者的異常回滾不會影響到它 148 userService2.funRequireNew(); 149 //資料庫操�? 150 funNone(); 151 152 //丟擲unchecked異常,觸發回滾 153 throwExcp(); 154 155 156 } 157 158 @Override 159 @Transactional 160 public void fun6() throws Exception { 161 162 funNone(); 163 164 userService2.funRequireException(); 165 166 } 167 168 @Override 169 @Transactional 170 public void fun6_2() throws Exception { 171 172 funNone(); 173 174 try { 175 //當呼叫的是Required�?,就算異常被處理了,整個方法也將會回�? 176 userService2.funRequireException(); 177 } catch (Exception e) { 178 System.out.println(e.getMessage()); 179 } 180 funNone(); 181 } 182 183 184 @Override 185 @Transactional 186 public void fun7() throws Exception { 187 188 funRequire(); 189 190 try { 191 funNestException(); 192 } catch (Exception e) { 193 System.out.println(e.getMessage()); 194 195 } 196 197 funRequire(); 198 199 } 200 201 @Override 202 @Transactional 203 public void fun8() throws Exception { 204 ((IUserService) AopContext.currentProxy()).funRequire(); 205 206 try { 207 ((IUserService) AopContext.currentProxy()).funNestException(); 208 } catch (Exception e) { 209 System.out.println(e.getMessage()); 210 211 } 212 213 ((IUserService) AopContext.currentProxy()).funRequire(); 214 } 215 216 217 //不帶事務的方�? 218 public void funNone() throws Exception { 219 save(new UserEntity("IUserService_None")); 220 221 } 222 223 @Override 224 public void funNoneException() throws Exception { 225 save(new UserEntity("IUserService_NoneException")); 226 throwExcp(); 227 } 228 229 230 //啟動預設事務的方�? 231 @Transactional(propagation = Propagation.REQUIRED) 232 public void funRequire() throws Exception { 233 save(new UserEntity("IUserService_Require")); 234 235 } 236 237 //啟動預設事務的方�? 238 @Transactional(propagation = Propagation.REQUIRED) 239 public void funRequire2() throws Exception { 240 save(new UserEntity("IUserService_Require2")); 241 242 } 243 244 //啟動預設事務的方�?,丟擲RuntimeException 245 @Override 246 @Transactional(propagation = Propagation.REQUIRED) 247 public void funRequireException() throws Exception { 248 save(new UserEntity("IUserService_RequireException")); 249 250 throwExcp(); 251 252 } 253 254 //啟動巢狀事務的方�? 255 @Transactional(propagation = Propagation.NESTED) 256 public void funNest() throws Exception { 257 save(new UserEntity("IUserService_Nest")); 258 259 } 260 261 262 //啟動巢狀事務的方�?,但會丟擲異常 263 @Override 264 @Transactional(propagation = Propagation.NESTED) 265 public void funNestException() throws Exception { 266 save(new UserEntity("IUserService_NestException")); 267 throwExcp(); 268 } 269 270 //REQUIRES_NEW事務的方�? 271 @Transactional(propagation = Propagation.REQUIRES_NEW) 272 public void funRequireNew() throws Exception { 273 save(new UserEntity("IUserService_RequireNew")); 274 275 } 276 277 //REQUIRES_NEW事務的方�?,但會丟擲異常 278 @Override 279 @Transactional(propagation = Propagation.REQUIRES_NEW) 280 public void funRequireNewException() throws Exception { 281 save(new UserEntity("IUserService_RequireNewException")); 282 throwExcp(); 283 } 284 285 286 //丟擲異常 287 private void throwExcp() throws Exception { 288 throw new RuntimeException("IUserService_boom"); 289 } 290 291 //儲存資料 292 public int save(UserEntity userEntity) throws Exception { 293 userDAO.save(userEntity); 294 UserEntity ue = userDAO.selectById(userEntity.getId()); 295 System.out.println(ue); 296 return userEntity.getId(); 297 } 298 }IUserServiceImpl
1 package com.demo.service.impl; 2 3 import com.demo.UserEntity; 4 import com.demo.dao.IUserDAO; 5 import com.demo.service.IUserService2; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Propagation; 8 import org.springframework.transaction.annotation.Transactional; 9 10 import javax.annotation.Resource; 11 12 /** 13 * Created by zhw on 16/3/24. 14 */ 15 @Service 16 public class IUserService2Impl implements IUserService2 { 17 18 @Resource 19 IUserDAO userDAO; 20 21 public void funNone() throws Exception { 22 save(new UserEntity("IUserService2_none")); 23 24 } 25 26 27 @Transactional(propagation = Propagation.REQUIRED) 28 public void funRequire() throws Exception { 29 save(new UserEntity("IUserService2_require")); 30 31 } 32 33 @Transactional(propagation = Propagation.REQUIRED) 34 public void funRequire2() throws Exception { 35 save(new UserEntity("IUserService2_require2")); 36 37 } 38 39 @Override 40 @Transactional(propagation = Propagation.REQUIRED) 41 public void funRequireException() throws Exception { 42 save(new UserEntity("IUserService2_requireException")); 43 44 throwExcp(); 45 46 } 47 48 @Transactional(propagation = Propagation.NESTED) 49 public void funNest() throws Exception { 50 save(new UserEntity("IUserService2_nest")); 51 52 } 53 54 @Override 55 @Transactional(propagation = Propagation.NESTED) 56 public void funNestException() throws Exception { 57 save(new UserEntity("IUserService2_nestException")); 58 throwExcp(); 59 } 60 61 @Transactional(propagation = Propagation.REQUIRES_NEW) 62 public void funRequireNew() throws Exception { 63 save(new UserEntity("IUserService2_requireNew")); 64 65 } 66 67 @Override 68 @Transactional(propagation = Propagation.REQUIRES_NEW) 69 public void funRequireNewException() throws Exception { 70 save(new UserEntity("IUserService2_requireNewException")); 71 throwExcp(); 72 73 74 } 75 76 77 private void throwExcp() throws Exception { 78 throw new RuntimeException("IUserService2_boom"); 79 } 80 81 public int save(UserEntity userEntity) throws Exception { 82 userDAO.save(userEntity); 83 return userEntity.getId(); 84 } 85 }IUserService2Impl
1 package com.demo.dao; 2 3 import com.demo.UserEntity; 4 5 /** 6 * Created by zhw on 16/3/24. 7 */ 8 public interface IUserDAO { 9 10 public int save(UserEntity userEntity) throws Exception; 11 12 public UserEntity selectById(int id) throws Exception; 13 }IUserDAO
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mapping.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 4 5 <mapper namespace="com.zhw.dao.IUserDAO"> 6 7 8 <insert id="save" useGeneratedKeys="true" 9 keyProperty="id" parameterType="com.zhw.UserEntity"> 10 11 INSERT 12 INTO 13 user 14 (name) 15 VALUES 16 (#{name}) 17 18 19 </insert> 20 21 <select id="selectById" resultType="com.zhw.UserEntity"> 22 select * from user where id = #{id}; 23 </select> 24 25 26 </mapper>user.xml