Explorar o código

事务的传播行为 测试

zxy %!s(int64=2) %!d(string=hai) anos
pai
achega
7c2a9850d9

+ 15 - 3
spring_aop/pom.xml

@@ -33,9 +33,16 @@
33 33
             <scope>test</scope>
34 34
         </dependency>
35 35
         <dependency>
36
-            <groupId>log4j</groupId>
37
-            <artifactId>log4j</artifactId>
38
-            <version>1.2.17</version>
36
+            <groupId>org.apache.logging.log4j</groupId>
37
+            <artifactId>log4j-core</artifactId>
38
+            <version>2.7</version>
39
+            <scope>provided</scope>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>org.apache.logging.log4j</groupId>
43
+            <artifactId>log4j-api</artifactId>
44
+            <version>2.7</version>
45
+            <scope>provided</scope>
39 46
         </dependency>
40 47
         <dependency>
41 48
             <groupId>junit</groupId>
@@ -68,6 +75,11 @@
68 75
             <artifactId>mysql-connector-java</artifactId>
69 76
             <version>8.0.17</version>
70 77
         </dependency>
78
+        <dependency>
79
+            <groupId>org.slf4j</groupId>
80
+            <artifactId>slf4j-nop</artifactId>
81
+            <version>1.7.2</version>
82
+        </dependency>
71 83
     </dependencies>
72 84
 
73 85
 </project>

+ 13 - 3
spring_aop/src/main/java/com/TestTx.java

@@ -1,12 +1,16 @@
1 1
 package com;
2 2
 
3 3
 
4
+import com.transaction_anno.entity.AccountEntity;
4 5
 import com.transaction_anno.service.AccountService;
6
+import com.transaction_anno.service.AccountTwoService;
5 7
 import org.junit.Test;
6 8
 import org.junit.runner.RunWith;
7 9
 import org.springframework.beans.factory.annotation.Autowired;
8 10
 import org.springframework.test.context.ContextConfiguration;
9 11
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12
+import org.springframework.transaction.annotation.Propagation;
13
+import org.springframework.transaction.annotation.Transactional;
10 14
 
11 15
 @RunWith(SpringJUnit4ClassRunner.class)
12 16
 @ContextConfiguration("classpath:context.xml")
@@ -14,10 +18,16 @@ public class TestTx {
14 18
 
15 19
     @Autowired
16 20
     AccountService accountService;
17
-
21
+    @Autowired
22
+    AccountTwoService accountTwoService;
18 23
     @Test
19 24
     public void transformAnno() {
20
-        // 交易测试
21
-        accountService.transfer(16,17,3.0);
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);
22 32
     }
23 33
 }

+ 7 - 0
spring_aop/src/main/java/com/transaction_/service/AccountTwoService.java

@@ -0,0 +1,7 @@
1
+package com.transaction_.service;
2
+
3
+import com.transaction_.entity.AccountEntity;
4
+
5
+public interface AccountTwoService {
6
+    void addAccountBalance(AccountEntity addEntity);
7
+}

+ 18 - 0
spring_aop/src/main/java/com/transaction_/service/impl/AccountTwoServiceImpl.java

@@ -0,0 +1,18 @@
1
+package com.transaction_.service.impl;
2
+
3
+import com.transaction_.dao.AccountDao;
4
+import com.transaction_.entity.AccountEntity;
5
+import com.transaction_.service.AccountTwoService;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+import org.springframework.transaction.annotation.Transactional;
9
+@Service
10
+public class AccountTwoServiceImpl implements AccountTwoService {
11
+    @Autowired
12
+    AccountDao accountDao;
13
+    @Override
14
+    @Transactional(rollbackFor = Exception.class)
15
+    public void addAccountBalance(AccountEntity addEntity) {
16
+        int in = accountDao.incomeAccount(addEntity, "in");
17
+    }
18
+}

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

@@ -1,7 +1,9 @@
1 1
 package com.transaction_anno.service;
2 2
 
3
+import com.transaction_anno.entity.AccountEntity;
4
+
3 5
 public interface AccountService {
4 6
       void initAccount();
5
-     // 模拟转账操作
6
-      boolean transfer(Integer fromId,Integer toId,Double balance);
7
+     // 扣除
8
+      int reduceAccount(AccountEntity toEntity);
7 9
 }

+ 7 - 0
spring_aop/src/main/java/com/transaction_anno/service/AccountTwoService.java

@@ -0,0 +1,7 @@
1
+package com.transaction_anno.service;
2
+
3
+import com.transaction_anno.entity.AccountEntity;
4
+
5
+public interface AccountTwoService {
6
+    void addAccountBalance(AccountEntity addEntity);
7
+}

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

@@ -1,5 +1,6 @@
1 1
 package com.transaction_anno.service.impl;
2 2
 
3
+import com.transaction_anno.service.AccountTwoService;
3 4
 import com.transaction_anno.dao.AccountDao;
4 5
 import com.transaction_anno.entity.AccountEntity;
5 6
 import com.transaction_anno.service.AccountService;
@@ -12,7 +13,6 @@ import org.springframework.transaction.annotation.Transactional;
12 13
 public class AccountServiceImpl implements AccountService {
13 14
     @Autowired
14 15
     AccountDao accountDao;
15
-
16 16
     @Override
17 17
     public void initAccount() {
18 18
         AccountEntity accountEntity = new AccountEntity();
@@ -22,25 +22,8 @@ public class AccountServiceImpl implements AccountService {
22 22
     }
23 23
     @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24 24
     @Override
25
-    public boolean transfer(Integer fromId, Integer toId, Double balance) {
26
-        // 转账操作
27
-        AccountEntity fromEntity = new AccountEntity();
28
-        AccountEntity toEntity = new AccountEntity();
29
-        fromEntity.setId(fromId).setBalance(balance);
30
-        toEntity.setId(toId).setBalance(balance);
31
-        // 开启事务
32
-        try {
33
-            toAccount(toEntity);
34
-        }catch (Exception e) {
35
-            System.out.println(e.getMessage());
36
-        }
37
-        int in = accountDao.incomeAccount(fromEntity, "in");
38
-        int i = 1/0;
39
-        // 提交事务
40
-        return false;
41
-    }
42
-    public int toAccount(AccountEntity toEntity) {
43
-        int to = accountDao.incomeAccount(toEntity, "to");
44
-        return to;
25
+    public int reduceAccount(AccountEntity toEntity) {
26
+        int in = accountDao.incomeAccount(toEntity, "to");
27
+        return 0;
45 28
     }
46 29
 }

+ 19 - 0
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTwoServiceImpl.java

@@ -0,0 +1,19 @@
1
+package com.transaction_anno.service.impl;
2
+
3
+import com.transaction_anno.dao.AccountDao;
4
+import com.transaction_anno.entity.AccountEntity;
5
+import com.transaction_anno.service.AccountTwoService;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+import org.springframework.transaction.annotation.Transactional;
9
+
10
+@Service("annoAccountTwo")
11
+public class AccountTwoServiceImpl implements AccountTwoService {
12
+    @Autowired
13
+    AccountDao accountDao;
14
+    @Override
15
+    @Transactional(rollbackFor = Exception.class)
16
+    public void addAccountBalance(AccountEntity addEntity) {
17
+        int in = accountDao.incomeAccount(addEntity, "in");
18
+    }
19
+}

+ 28 - 14
spring_aop/src/main/resources/log4j.properties

@@ -1,16 +1,30 @@
1
-log4j.rootCategory=INFO, stdout , R
2
-log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3
-log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
4
-log4j.appender.stdout.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
1
+log4j.rootLogger=INFO,Console
5 2
 
6
-log4j.appender.R=org.apache.log4j.RollingFileAppender
7
-log4j.appender.R.File=${\u5E94\u7528\u540D.root}/WEB-INF/logs/xx.log
8
-log4j.appender.R.MaxFileSize=1024KB
9
-log4j.appender.R.MaxBackupIndex=10
10
-log4j.appender.R.layout=org.apache.log4j.PatternLayout
11
-log4j.appender.R.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n
3
+log4j.appender.Console=org.apache.log4j.ConsoleAppender
4
+log4j.appender.Console.Target=System.out
5
+log4j.appender.Console.Threshold=DEBUG
6
+log4j.appender.Console.layout=org.apache.log4j.PatternLayout
7
+log4j.appender.Console.layout.ConversionPattern=[%d]%l%5p:%m%n
12 8
 
13
-log4j.logger.org.springframework=DEBUG
14
-log4j.logger.org.hibernate=DEBUG
15
-log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
16
-log4j.logger.com.lagooo.as=DEBUG
9
+log4j.appender.DebugFile=org.apache.log4j.RollingFileAppender
10
+log4j.appender.DebugFile.File=../log/debugFile.log
11
+#log4j.appender.DebugFile.File=debugFile.log
12
+log4j.appender.DebugFile.Append=true
13
+log4j.appender.DebugFile.Threshold=DEBUG
14
+log4j.appender.DebugFile.layout=org.apache.log4j.PatternLayout
15
+log4j.appender.DebugFile.layout.ConversionPattern=[%d]%l%5p:%m%n
16
+log4j.appender.DebugFile.MaxFileSize=20MB
17
+log4j.appender.DebugFile.MaxBackupIndex=10
18
+
19
+log4j.logger.com.ibatis=DEBUG
20
+log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
21
+log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
22
+log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
23
+
24
+log4j.logger.java.sql=DEBUG
25
+log4j.logger.java.sql.Connection = INFO
26
+log4j.logger.java.sql.Statement = DEBUG
27
+log4j.logger.java.sql.PreparedStatement = DEBUG
28
+log4j.logger.java.sql.ResultSet = DEBUG
29
+
30
+log4j.logger.com.yuetao=DEBUG

+ 29 - 0
spring_aop/src/main/resources/log4j2.xml

@@ -0,0 +1,29 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<Configuration>
3
+    <Appenders>
4
+        <Console name="STDOUT" target="SYSTEM_OUT">
5
+            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
6
+        </Console>
7
+        <RollingFile name="RollingFile" fileName="logs/strutslog1.log"
8
+                     filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
9
+            <PatternLayout>
10
+                <Pattern>%d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n</Pattern>
11
+            </PatternLayout>
12
+            <Policies>
13
+                <TimeBasedTriggeringPolicy />
14
+                <SizeBasedTriggeringPolicy size="1 KB"/>
15
+            </Policies>
16
+            <DefaultRolloverStrategy fileIndex="max" max="2"/>
17
+        </RollingFile>
18
+    </Appenders>
19
+    <Loggers>
20
+        <Logger name="com.opensymphony.xwork2" level="WAN"/>
21
+        <Logger name="org.apache.struts2" level="WAN"/>
22
+        <Logger name="org.springframework.jdbc.core" level="debug"/>
23
+        <Logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="trace"/>
24
+        <Root level="warn">
25
+            <AppenderRef ref="STDOUT"/>
26
+        </Root>
27
+    </Loggers>
28
+
29
+</Configuration>