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 }