yantian yue
2023-10-20 4f58ba24b9a7e24dc38aa8eb9ca0b92c83e161ac
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.modular.demos.service;
2
3 import cn.hutool.core.util.RandomUtil;
4 import cn.stylefeng.guns.sys.modular.system.entity.User;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Service;
7 import org.springframework.transaction.annotation.Transactional;
8
9 import java.util.Date;
10
11 /**
12  * <p>
13  * 测试数据源能否回滚的例子
14  * </p>
15  *
16  * @author stylefeng
17  * @since 2018-12-07
18  */
19 @Service
20 public class TranTestService {
21
22     @Autowired
23     private GunsDbService gunsDbService;
24
25     @Autowired
26     private OtherDbService otherDbService;
27
28     @Transactional(rollbackFor = Exception.class)
29     public void testSingleSuccess() {
30
31         User user = new User();
32         user.setAccount(RandomUtil.randomString(5));
33         user.setPassword(RandomUtil.randomString(5));
34         user.setCreateTime(new Date());
35         user.setUpdateTime(new Date());
36         user.setCreateUser(1L);
37         user.setUpdateUser(1L);
38         gunsDbService.save(user);
39
40         User user2 = new User();
41         user2.setAccount(RandomUtil.randomString(5));
42         user2.setPassword(RandomUtil.randomString(5));
43         user2.setCreateTime(new Date());
44         user2.setUpdateTime(new Date());
45         user2.setCreateUser(1L);
46         user2.setUpdateUser(1L);
47         gunsDbService.save(user2);
48     }
49
50     @Transactional(rollbackFor = Exception.class)
51     public void testSingleFail() {
52
53         User user = new User();
54         user.setAccount(RandomUtil.randomString(5));
55         user.setPassword(RandomUtil.randomString(5));
56         user.setCreateTime(new Date());
57         user.setUpdateTime(new Date());
58         user.setCreateUser(1L);
59         user.setUpdateUser(1L);
60         gunsDbService.save(user);
61
62         User user2 = new User();
63         user2.setAccount(RandomUtil.randomString(5));
64         user2.setPassword(RandomUtil.randomString(5));
65         user2.setCreateTime(new Date());
66         user2.setUpdateTime(new Date());
67         user2.setCreateUser(1L);
68         user2.setUpdateUser(1L);
69         gunsDbService.save(user2);
70
71         int i = 1 / 0;
72     }
73
74     @Transactional(rollbackFor = Exception.class)
75     public void beginTest() {
76         gunsDbService.gunsdb();
77         otherDbService.otherdb();
78     }
79
80     @Transactional(rollbackFor = Exception.class)
81     public void beginTestFail() {
82         gunsDbService.gunsdb();
83         otherDbService.otherdb();
84         int i = 1 / 0;
85     }
86
87 }