spring 事务 demo
Posted toupdating
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 事务 demo相关的知识,希望对你有一定的参考价值。
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; 8 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 }
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 }
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 }
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>
以上是关于spring 事务 demo的主要内容,如果未能解决你的问题,请参考以下文章