一种 Service 方法调用 Spring 事务的内部多个方法 [英] One Service method invoke inner multiple method for Spring transaction

查看:34
本文介绍了一种 Service 方法调用 Spring 事务的内部多个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.bluesky;公共接口 FooServiceIface {public void insertA();公共无效插入B();}

<小时>

package com.bluesky;导入 org.springframework.jdbc.core.support.JdbcDaoSupport;公共类 FooServiceImpl 扩展 JdbcDaoSupport 实现 FooServiceIface {公共无效插入A(){this.getJdbcTemplate().execute("插入学生(姓名)值('stuA')");插入B();int i=10/0;}公共无效插入B(){this.getJdbcTemplate().execute("insert student(name) values('stuB')");}}

<小时>

公共类客户端{公共静态无效主(字符串 [] args){ApplicationContext appContxt = new ClassPathXmlApplicationContext("applicationContext.xml");FooServiceIface fService= (FooServiceIface)appContxt.getBean("fooService");fService.insertA();}}

<小时>

抱歉代码复杂

当我运行客户端吹调试日志时显示:

<前>21:44:19,546 DEBUG TransactionSynchronizationManager:183 - 键 [org.springframework.jdbc.datasource.DriverManagerDataSource@1b9658e] 的绑定值 [org.springframework.jdbc.datasource.ConnectionHolder@ba86ef] 到线程 [main]21:44:19,546 DEBUG TransactionSynchronizationManager:258 - 初始化事务同步21:44:19,547 DEBUG TransactionInterceptor:362 - 获取 [com.bluesky.FooServiceImpl.insertA] 的交易21:44:19,547 DEBUG JdbcTemplate:416 - 执行 SQL 语句 [插入学生(姓名)值('stuA')]21:44:19,592 DEBUG JdbcTemplate:416 - 执行 SQL 语句 [插入学生(姓名)值('stuB')]21:44:19,594 DEBUG TransactionInterceptor:406 - 在异常之后完成 [com.bluesky.FooServiceImpl.insertA] 的事务:java.lang.ArithmeticException:/为零21:44:19,594 DEBUG RuleBasedTransactionAttribute:130 - 应用规则来确定事务是否应该在 java.lang.ArithmeticException 上回滚:/为零21:44:19,594 DEBUG RuleBasedTransactionAttribute:147 - 获胜回滚规则为:空21:44:19,595 DEBUG RuleBasedTransactionAttribute:152 - 找不到相关的回滚规则:应用默认规则21:44:19,595 DEBUG DataSourceTransactionManager:938 - 完成同步前触发21:44:19,595 DEBUG DataSourceTransactionManager:843 - 启动事务回滚21:44:19,596 DEBUG DataSourceTransactionManager:279 - 在连接上回滚 JDBC 事务 [com.mysql.jdbc.JDBC4Connection@167f4bf]21:44:19,598 DEBUG DataSourceTransactionManager:967 - 完成同步后触发21:44:19,598 DEBUG TransactionSynchronizationManager:316 - 清除事务同步

发现调用insertA()方法时,该方法启动一个事务,当代码到达insertB()时,没有启动事务.>

是否有什么我没有配置或我做错了任何问题

我的意图是当 insertA() 调用 insertB() 时有一个 REQUIRES_NEW 事务开始.

解决方案

KLE 关于重构代码的建议在金钱上是正确的,但至于 为什么它不起作用,Spring AOP 使用 JDK 动态代理 默认提供 AOP.这意味着当您将服务注入某物时,真正注入的是一个实现您的服务接口的 Proxy 实例.当在此代理上调用方法时,它会在委托给您的实际服务实例之前运行事务代码.但是,一旦控制流在您的服务内部,通过 this.foo() 调用另一个方法(即使 this 是隐式的)只会在同一实例上调用一个方法: 你的服务.它不会返回到代理,代理是唯一知道交易的东西.如果你切换了 使用 AspectJ 构建或加载时字节码编织,然后您可以这样做,它会按预期工作,因为事务调用代码将直接编织到您的服务代码中,而不是存在于单独的对象中.

package com.bluesky;

public interface FooServiceIface {
    public  void insertA();
    public void insertB();
}


package com.bluesky;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class FooServiceImpl extends JdbcDaoSupport implements FooServiceIface {

    public void insertA() {
        this.getJdbcTemplate().execute("insert student(name) values('stuA')");
         insertB();
         int i=10/0;
    }

    public void insertB() {
        this.getJdbcTemplate().execute("insert student(name) values('stuB')");

    }

}


public class Client {

    public static void main(String[] args) {

        ApplicationContext appContxt = new ClassPathXmlApplicationContext("applicationContext.xml");

        FooServiceIface  fService= (FooServiceIface)appContxt.getBean("fooService");

        fService.insertA();

    }
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"/>
       <property name="username" value="root"/>
       <property name="password" value="root"/>
    </bean>

    <bean id="fooService" class="com.bluesky.FooServiceImpl">
         <property name="dataSource" ref="dataSource"/>  
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource"/>  
    </bean>  

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insertA" propagation="REQUIRED" />
             <tx:method name="insertB" propagation="REQUIRES_NEW" />
        </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="true">
        <aop:pointcut id="interceptorPointCuts" expression="execution(* com.bluesky.*Service*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />       
    </aop:config>     
</beans>

Sorry for the Complicated code

When i run Client blow debug logs are showing:

21:44:19,546 DEBUG TransactionSynchronizationManager:183 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@ba86ef] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@1b9658e] to thread [main]
21:44:19,546 DEBUG TransactionSynchronizationManager:258 - Initializing transaction synchronization
21:44:19,547 DEBUG TransactionInterceptor:362 - Getting transaction for [com.bluesky.FooServiceImpl.insertA]
21:44:19,547 DEBUG JdbcTemplate:416 - Executing SQL statement [insert student(name) values('stuA')]
21:44:19,592 DEBUG JdbcTemplate:416 - Executing SQL statement [insert student(name) values('stuB')]
21:44:19,594 DEBUG TransactionInterceptor:406 - Completing transaction for [com.bluesky.FooServiceImpl.insertA] after exception: java.lang.ArithmeticException: / by zero
21:44:19,594 DEBUG RuleBasedTransactionAttribute:130 - Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero
21:44:19,594 DEBUG RuleBasedTransactionAttribute:147 - Winning rollback rule is: null
21:44:19,595 DEBUG RuleBasedTransactionAttribute:152 - No relevant rollback rule found: applying default rules
21:44:19,595 DEBUG DataSourceTransactionManager:938 - Triggering beforeCompletion synchronization
21:44:19,595 DEBUG DataSourceTransactionManager:843 - Initiating transaction rollback
21:44:19,596 DEBUG DataSourceTransactionManager:279 - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@167f4bf]
21:44:19,598 DEBUG DataSourceTransactionManager:967 - Triggering afterCompletion synchronization
21:44:19,598 DEBUG TransactionSynchronizationManager:316 - Clearing transaction synchronization

I found that when invoked insertA() method , this method start a transaction, when the code arrive insertB(), there is not transaction for starting.

Is there anything did i not configured or any concern i made mistaken

My intention is to when insertA() call insertB() there is an REQUIRES_NEW transaction start.

解决方案

KLE's advice about refactoring your code is right on the money, but as for why it's not working, Spring AOP uses JDK dynamic proxies by default to provide AOP. That means that when you inject your service into something, what's really getting injected is a Proxy instance that implements your service interface. When a method is invoked on this proxy, it runs the transaction code before delegating to your actual service instance. Once the control flow is inside your service, though, calling another method via this.foo() (even if the this is implicit) just invokes a method on that same instance: your service. It doesn't go back to the proxy, which is the only thing that knows about transactions. If you switched to build- or load-time bytecode weaving with AspectJ, then you could do this, and it would work as expected, since the transaction-invoking code would be woven directly into your service code instead of living in a separate object.

这篇关于一种 Service 方法调用 Spring 事务的内部多个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆