浏览代码

事务传播行为测试

zhangxiaoyu 2 年之前
父节点
当前提交
2a33b6d9e3

+ 0 - 1
spring_aop/pom.xml

@@ -78,7 +78,6 @@
78 78
         <dependency>
79 79
             <groupId>org.slf4j</groupId>
80 80
             <artifactId>slf4j-nop</artifactId>
81
-            <version>1.7.2</version>
82 81
         </dependency>
83 82
     </dependencies>
84 83
 

+ 4 - 15
spring_aop/src/main/java/com/TestTx.java

@@ -1,33 +1,22 @@
1 1
 package com;
2 2
 
3 3
 
4
-import com.transaction_anno.entity.AccountEntity;
5
-import com.transaction_anno.service.AccountService;
6
-import com.transaction_anno.service.AccountTwoService;
4
+import com.transaction_anno.service.AccountTxService;
7 5
 import org.junit.Test;
8 6
 import org.junit.runner.RunWith;
9 7
 import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.test.annotation.Commit;
10 9
 import org.springframework.test.context.ContextConfiguration;
11 10
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12
-import org.springframework.transaction.annotation.Propagation;
13 11
 import org.springframework.transaction.annotation.Transactional;
14 12
 
15 13
 @RunWith(SpringJUnit4ClassRunner.class)
16 14
 @ContextConfiguration("classpath:context.xml")
17 15
 public class TestTx {
18
-
19
-    @Autowired
20
-    AccountService accountService;
21 16
     @Autowired
22
-    AccountTwoService accountTwoService;
17
+    AccountTxService accountTxService;
23 18
     @Test
24 19
     public void transformAnno() {
25
-        // 转账操作
26
-        AccountEntity reduceEntity = new AccountEntity();
27
-        AccountEntity addEntity = new AccountEntity();
28
-        reduceEntity.setId(16).setBalance(3.0);
29
-        addEntity.setId(17).setBalance(3.0);
30
-        accountService.reduceAccount(reduceEntity);
31
-        accountTwoService.addAccountBalance(addEntity);
20
+        accountTxService.transformTx();
32 21
     }
33 22
 }

+ 5 - 0
spring_aop/src/main/java/com/transaction_anno/service/AccountTxService.java

@@ -0,0 +1,5 @@
1
+package com.transaction_anno.service;
2
+
3
+public interface AccountTxService {
4
+    void transformTx();
5
+}

+ 4 - 2
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java

@@ -20,10 +20,12 @@ public class AccountServiceImpl implements AccountService {
20 20
         accountEntity.setBalance(300.0);
21 21
         int i = accountDao.saveAccount(accountEntity);
22 22
     }
23
-    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
23
+//    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24
+    @Transactional
24 25
     @Override
25 26
     public int reduceAccount(AccountEntity toEntity) {
26 27
         int in = accountDao.incomeAccount(toEntity, "to");
27
-        return 0;
28
+        System.out.println(1/0);
29
+        return in;
28 30
     }
29 31
 }

+ 35 - 0
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTxServiceImpl.java

@@ -0,0 +1,35 @@
1
+package com.transaction_anno.service.impl;
2
+
3
+import com.transaction_anno.entity.AccountEntity;
4
+import com.transaction_anno.service.AccountService;
5
+import com.transaction_anno.service.AccountTwoService;
6
+import com.transaction_anno.service.AccountTxService;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.stereotype.Service;
9
+import org.springframework.transaction.annotation.Propagation;
10
+import org.springframework.transaction.annotation.Transactional;
11
+
12
+@Service
13
+public class AccountTxServiceImpl implements AccountTxService {
14
+    @Autowired
15
+    AccountService accountService;
16
+    @Autowired
17
+    AccountTwoService accountTwoService;
18
+
19
+//    @Transactional(propagation = Propagation.REQUIRED)
20
+    @Transactional
21
+    @Override
22
+    public void transformTx() {
23
+        // 转账操作
24
+        AccountEntity reduceEntity = new AccountEntity();
25
+        AccountEntity addEntity = new AccountEntity();
26
+        reduceEntity.setId(16).setBalance(3.0);
27
+        addEntity.setId(17).setBalance(3.0);
28
+//        try {
29
+//        }catch (Exception e) {
30
+//            System.out.println(e.getMessage());
31
+//        }
32
+        accountService.reduceAccount(reduceEntity);
33
+        accountTwoService.addAccountBalance(addEntity);
34
+    }
35
+}