简体   繁体   English

在 Spring Boot 应用程序中使用方面回滚事务

[英]Rolling back transaction with aspect in Spring boot application

I am new to the concept of Aspect-Oriented Programming.我是面向切面编程概念的新手。 I am writing this following aspect using AspectJ in my spring boot application:我正在我的 Spring Boot 应用程序中使用 AspectJ 编写以下方面:

@Aspect
public class MyAspect {

  private final AspectUtil aspectUtil;

  public MyAspect(AspectUtil aspectUtil)) {
    this.aspectUtil = aspectUtil;
  }

  @Pointcut("@within(org.springframework.stereotype.Service)")
  public void applicationServicePointcut() {
  }
  @AfterReturning(value = ("applicationServicePointcut()"))
  public void process(JoinPoint joinPoint) {
    HttpServletRequest request = aspectUtil.getRequest();
    aspectUtil.getHttpVerb(request);
    processUtil(request, joinPoint);
  }

  public void processUtil(HttpServletRequest request) {
    ...
}

All of the services defined in my application are annotated with @Transactional , one example is given below:我的应用程序中定义的所有服务都用@Transactional进行了注释,下面给出了一个示例:

@Service
@Transactional
public class MyService {
 ..
}

So, here my question is if any exception I throw from my aspect as defined above, then would the associated transaction in the service layer get rolled back?所以,在这里我的问题是,如果我从上面定义的方面抛出任何异常,那么服务层中的关联事务会被回滚吗?

Actually, my goal is to roll back the entire transaction in case, any exception occurs at this aspect.实际上,我的目标是回滚整个事务,以防在这方面发生任何异常。

An example for a similar requirement 1.4.8.类似要求的示例1.4.8。 Advising Transactional Operations is available in the documentation.文档中提供了建议交易操作

Exerpt :摘录:

The ordering of advice is controlled through the Ordered interface.通知的排序是通过 Ordered 接口控制的。 ......... The result of the preceding configuration is a fooService bean that has profiling and transactional aspects applied to it in that order. ......... 前面配置的结果是一个 fooService bean,它按顺序应用了分析和事务方面。 If you want the profiling advice to run after the transactional advice on the way in and before the transactional advice on the way out, you can swap the value of the profiling aspect bean's order property so that it is higher than the transactional advice's order value...如果您希望分析建议在进入事务建议之后和退出事务建议之前运行,您可以交换分析方面 bean 的 order 属性的值,使其高于事务建议的 order 值。 ..

Please do go through : Advice Ordering to understand the concept better and control the order of advice.请通过: Advice Ordering来更好地理解概念并控制通知的顺序。

Also go through the conceptual view of calling a method on a transactional proxy:还通过在事务代理上调用方法的概念视图: 在此处输入图片说明

Basically the idea would be to throw the exception from the custom aspect that should result in the rollback from the Transaction Advisor .基本上这个想法是从应该导致从Transaction Advisor回滚的自定义方面抛出异常。

The short answer is it depends.简短的回答是这取决于。 Aspects are ordered in Spring, meaning that they run with a specific order.方面在 Spring 中是有序的,这意味着它们以特定的顺序运行。 That means that if your aspect is set to run before the transactional aspect, then no transaction will be created and there would be nothing to actually rollback.这意味着,如果您的方面设置为在transactional方面之前运行,那么将不会创建任何事务,也不会实际回滚任何内容。

On the other hand, if you set this to run after the transaction aspect and it indeed throws an exception, this would automatically mean that the transaction would have ran and would be committed.另一方面,如果您将 this 设置为在事务方面之后运行并且它确实抛出了异常,则这将自动意味着事务将运行并被提交。

I believe you need to check on ordering of aspects to find the pattern that suits your needs the best.我相信您需要检查方面的排序以找到最适合您需求的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM