首页 > 其他分享 >spring---->Spring事务与ApplicationEventPublisher

spring---->Spring事务与ApplicationEventPublisher

时间:2023-04-06 22:46:58浏览次数:31  
标签:ApplicationEventPublisher spring Transactional ---- AccountEvent new save saved 

Spring事务与ApplicationEventPublisher

@Transactional
public void handle() {
    var account = Account.builder()
            .username("huhx")
            .password("pass")
            .build();
    accountRepository.save(account);
    publisher.publishEvent(new AccountEvent("huhx"));
    var account2 = Account.builder()
            .username("huhx2")
            .password("pass2")
            .build();
    accountRepository.save(account2);
}

@Component
@RequiredArgsConstructor
public class AccountEventHandler {

    private final BookService bookService;

    @EventListener(AccountEvent.class)
    public void onAccountEvent(AccountEvent event) {
        bookService.save(new BookRequest(event.username()));

        throw new RuntimeException();
    }
}

// 1. No books saved, no account saved

// 2. add @Transactional on onAccountEvent, No books saved, no account saved

// 3. add @Async on onAccountEvent, two accounts saved, one book saved

// 4. add @Async and @Transactional on onAccountEvent, only two accounts saved

// 5. execute in Thread and with @Transactional, two accounts saved, one book saved(这里加不加@Transactional,没区别)
@Transactional
@EventListener(AccountEvent.class)
public void handle(AccountEvent event) {
    new Thread(() -> {
        bookService.save(new BookRequest(event.username()));
        throw new RuntimeException();
    }).start();
}

// 6. change the eventHandle code as following, no accounts saved, only one book saved(这里加不加@Transactional,没区别)
@Transactional
@EventListener(AccountEvent.class)
public void handle(AccountEvent event) {
    new Thread(() -> bookService.save(new BookRequest(event.username()))).start();
    bookService.save(new BookRequest(event.username()));
    throw new RuntimeException();
}

标签:ApplicationEventPublisher,spring,Transactional,----,AccountEvent,new,save,saved,
From: https://www.cnblogs.com/huhx/p/17266959.html

相关文章

  • org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationExcept
    错误信息:com.xubo.rabbitmq.springbootrabbitmq.SpringbootRabbitmqApplication._________/\\/___'_____(_)______\\\\(()\___|'_|'_||'_\/_`|\\\\\\/___)||_)||||......
  • 剑指 Offer 14- II. 剪绳子 II
    题目链接:剑指Offer14-II.剪绳子II方法:数论解题思路将\(n\)分为\(m\)个数的和,使得这\(m\)个数的乘积最大,那么应该将\(m\)个数分为\(2\)和\(3\)的组合,尽可能为\(3\)。注意大数越界问题。代码classSolution{public:intcuttingRope(intn){......
  • Java方法
    Java方法Java方法是语句的集合,它们在一起执行一个功能。方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法在程序中被创建,在其他地方被引用设计方法的原则:方法的本意是功能块,就是实现某个功能的语句块的集合。我们设计方法的时候,最好保持方法的原子性,就是一个方......
  • Exp4 恶意代码分析
    一、实验目的1.是监控你自己系统的运行状态,看有没有可疑的程序在运行。2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件;分析工具尽量使用原生指令或sysinternals,systracer套件。3.假定将来工作中你觉得自己的主机有问题,就可以用实验中的这个思路,先整个系统监控看能不能找......
  • 为ViewPager设置SimpleViewPagerIndicator
    说到ViewPagerIndicator,网上有很多专门的开源库;我这里重提一下,只是想试试它的实现方法;记录下来,可以以后快速的修改迭代~~~很简单的一个类:importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importan......
  • 3.哈密顿绕行世界问题
    原题链接:https://www.acwing.com/problem/content/description/4232/思路很像全排列,运用压缩状态存储,注意细节#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=25;boolst[N][N];intpath[N];intn,cnt;voiddfs(intu,int......
  • 数据库系统原理之数据管理技术的发展
    数据管理技术的发展第一节数据库技术发展概述数据模型是数据库系统的核心和基础以数据模型的发展为主线,数据库技术可以相应地分为三个发展阶段:第一代的网状、层次数据库系统第二代的关系数据库系统新一代的数据库系统一、第一代数据库系统层次数据库系统层次模......
  • 一个不错的ArcMenu
    ArcMenu这种效果现在很多人都实现了而且代码质量也不错packagecom.example.zhy_arcmenu;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.util.Typ......
  • flask请求上下文分析,源码request原理,wtforms,精确导出依赖,函数和方法,threading.locl对
    内容回顾蓝图第一步:导入第二步:实例化得到对象,可以指定static和templates第三步:app中注册蓝图,注册蓝图时,可以指定前缀第四步:使用蓝图,注册路由,注册请求扩展g对象当次请求的全局对象,在当次请求中可以放值和取值跟session的区别是session可以在多次请求中使用,g对象只在当次请......
  • SwipeRefreshLayout和ListView的EmptyView共存冲突的问题
    SwipeRefreshLayout是android官方的下拉刷新控件;它内部有且只能有一个子控件;当一个ListView嵌入到它内部时,就不能为ListView带一个EmptyView了;于是很自然的想到将ListView和EmptyView纳入到一个父控件中;典型的像下面这样的布局:<android.support.v4.......