Browse Source

测试了xml配置方法事务隔离

zxy 2 years ago
parent
commit
ede79ab872

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

@@ -1,7 +1,7 @@
1
 package com.transaction_.service;
1
 package com.transaction_.service;
2
 
2
 
3
 public interface AccountService {
3
 public interface AccountService {
4
-     void initAccount();
4
+     public void initAccount();
5
      // 模拟转账操作
5
      // 模拟转账操作
6
-     boolean transfer(Integer fromId,Integer toId,Double balance);
6
+     public boolean transfer(Integer fromId,Integer toId,Double balance);
7
 }
7
 }

+ 5 - 2
spring_aop/src/main/java/com/transaction_/service/impl/AccountServiceImpl.java

@@ -3,6 +3,7 @@ package com.transaction_.service.impl;
3
 import com.transaction_.dao.AccountDao;
3
 import com.transaction_.dao.AccountDao;
4
 import com.transaction_.entity.AccountEntity;
4
 import com.transaction_.entity.AccountEntity;
5
 import com.transaction_.service.AccountService;
5
 import com.transaction_.service.AccountService;
6
+import org.springframework.transaction.annotation.Propagation;
6
 import org.springframework.transaction.annotation.Transactional;
7
 import org.springframework.transaction.annotation.Transactional;
7
 
8
 
8
 public class AccountServiceImpl implements AccountService {
9
 public class AccountServiceImpl implements AccountService {
@@ -18,17 +19,19 @@ public class AccountServiceImpl implements AccountService {
18
         accountEntity.setBalance(300.0);
19
         accountEntity.setBalance(300.0);
19
         int i = accountDao.saveAccount(accountEntity);
20
         int i = accountDao.saveAccount(accountEntity);
20
     }
21
     }
21
-    @Transactional(rollbackFor = Exception.class)
22
+    @Transactional(propagation = Propagation.SUPPORTS)
22
     @Override
23
     @Override
23
     public boolean transfer(Integer fromId, Integer toId, Double balance) {
24
     public boolean transfer(Integer fromId, Integer toId, Double balance) {
24
         // 转账操作
25
         // 转账操作
26
+        boolean b;
27
+        int i1 = 11;
25
         AccountEntity fromEntity = new AccountEntity();
28
         AccountEntity fromEntity = new AccountEntity();
26
         AccountEntity toEntity = new AccountEntity();
29
         AccountEntity toEntity = new AccountEntity();
27
         fromEntity.setId(fromId).setBalance(balance);
30
         fromEntity.setId(fromId).setBalance(balance);
28
         toEntity.setId(toId).setBalance(balance);
31
         toEntity.setId(toId).setBalance(balance);
29
         // 开启事务
32
         // 开启事务
30
         accountDao.incomeAccount(toEntity,"to");
33
         accountDao.incomeAccount(toEntity,"to");
31
-//        int i = 1/0;
34
+        int i = 1/0;
32
         accountDao.incomeAccount(fromEntity,"in");
35
         accountDao.incomeAccount(fromEntity,"in");
33
         // 提交事务
36
         // 提交事务
34
         return false;
37
         return false;

+ 15 - 1
spring_aop/src/main/resources/transaction_aop.xml

@@ -3,12 +3,15 @@
3
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
        xmlns:tx="http://www.springframework.org/schema/tx"
4
        xmlns:tx="http://www.springframework.org/schema/tx"
5
        xmlns:context="http://www.springframework.org/schema/context"
5
        xmlns:context="http://www.springframework.org/schema/context"
6
+       xmlns:aop="http://www.springframework.org/schema/aop"
6
        xsi:schemaLocation="http://www.springframework.org/schema/beans
7
        xsi:schemaLocation="http://www.springframework.org/schema/beans
7
        http://www.springframework.org/schema/beans/spring-beans.xsd
8
        http://www.springframework.org/schema/beans/spring-beans.xsd
8
        http://www.springframework.org/schema/tx
9
        http://www.springframework.org/schema/tx
9
        https://www.springframework.org/schema/tx/spring-tx.xsd
10
        https://www.springframework.org/schema/tx/spring-tx.xsd
10
        http://www.springframework.org/schema/context
11
        http://www.springframework.org/schema/context
11
        https://www.springframework.org/schema/context/spring-context.xsd
12
        https://www.springframework.org/schema/context/spring-context.xsd
13
+       http://www.springframework.org/schema/aop
14
+       https://www.springframework.org/schema/aop/spring-aop.xsd
12
 ">
15
 ">
13
     <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
16
     <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
14
     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
17
     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
@@ -16,6 +19,7 @@
16
         <property name="url" value="${jdbc.url}"></property>
19
         <property name="url" value="${jdbc.url}"></property>
17
         <property name="username" value="${jdbc.username}"></property>
20
         <property name="username" value="${jdbc.username}"></property>
18
         <property name="password" value="${jdbc.password}"></property>
21
         <property name="password" value="${jdbc.password}"></property>
22
+<!--        <property name="defaultAutoCommit" value="false"></property>-->
19
     </bean>
23
     </bean>
20
     <bean id="jdbcTemp" class="org.springframework.jdbc.core.JdbcTemplate">
24
     <bean id="jdbcTemp" class="org.springframework.jdbc.core.JdbcTemplate">
21
         <property name="dataSource" ref="dataSource"></property>
25
         <property name="dataSource" ref="dataSource"></property>
@@ -27,6 +31,16 @@
27
         <property name="accountDao" ref="accountDao">
31
         <property name="accountDao" ref="accountDao">
28
         </property>
32
         </property>
29
     </bean>
33
     </bean>
30
-
34
+    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
35
+        <property name="dataSource" ref="dataSource"></property>
36
+    </bean>
37
+<!--    <tx:advice id="interceptor" transaction-manager="transactionManager">-->
38
+<!--        <tx:attributes>-->
39
+<!--            <tx:method name="transfer" rollback-for="java.lang.Exception" timeout="-1"/>-->
40
+<!--        </tx:attributes>-->
41
+<!--    </tx:advice>-->
42
+<!--    <aop:config>-->
43
+<!--        <aop:advisor advice-ref="interceptor" pointcut="execution(* com.transaction_.service.impl.AccountServiceImpl.*(..))"></aop:advisor>-->
44
+<!--    </aop:config>-->
31
 
45
 
32
 </beans>
46
 </beans>