spring---->Spring事务与ApplicationEventPublisher
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的publishEvent方法是同步的
作者:
huhx
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
出处: www.cnblogs.com/huhx
格言:你尽力了,才有资格说自己的运气不好。
版权:本文版权归作者huhx和博客园共有,欢迎转载。未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。