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