编程式事务工具类
# TransactionalUtil
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
/**
* 编程式事务工具类
*/
@Component
public class TransactionalUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionalUtil.class);
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
/**
* 开启事务
*
* @return TransactionStatus
*/
public TransactionStatus begin(){
LOGGER.info("Start a programmatic transaction");
return dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
}
/**
* 提交事务
*
* @param transactionStatus transactionStatus
*/
public void commit(TransactionStatus transactionStatus) {
dataSourceTransactionManager.commit(transactionStatus);
LOGGER.info("transaction commit");
}
/**
* 回滚事务
*
* @param transactionStatus transactionStatus
*/
public void rollback(TransactionStatus transactionStatus) {
dataSourceTransactionManager.rollback(transactionStatus);
LOGGER.info("transaction rollback");
}
}
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
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
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
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
编辑 (opens new window)
上次更新: 2023/05/17, 22:21:48
- 01
- SpringCache基本配置类05-16
- 03
- Rpamis-security-原理解析12-13