如何仅在 spring-transactions 交易成功时发送电子邮件
How to SendEmail only if transaction is success with spring-transactions
我想在数据库中创建一个用户,并使用 AWS SES 向这个案例的用户发送电子邮件。
- 如果用户在数据库中成功提交事务 => 发送电子邮件
- 如果发送电子邮件 AWS SES 失败(检查异常)=> 回滚数据库中的用户创建
- 如果用户在数据库中提交事务失败 => 不要使用 AWS 向用户发送电子邮件
我的代码有问题:如果我的 sendEmail
方法抛出异常,则会提交事务。
配置:Spring-使用spring-data-jpa
启动项目
class EmailServiceImpl {
@Transactional(rollbackFor = Exception.class)
@Override
public User createUserAndSendEmail(UserDto userDto) throws UserEmailException {
try {
//rollback userCreation if sendEmail throw a checkedException
User user = userService.create(userDto);
sendEmail(user);
return user;
} catch (Exception exception) {
throw new UserEmailException(exception.getMessage());
}
}
//don't send email if userCommit in database failed
private void sendEmail(User user) {
amazonEmailService.sendMail(user);
}
}
class UserServiceImpl {
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public User create(UserDto userDto) throws Exception {
User user = mapp(userDto);
return userRepository.save(user);
}
}
要在 TX 提交后执行某些操作,您可以使用 @TransactionalEventListener
和 TransactionPhase.AFTER_COMMIT
(默认设置)。把你想做的动作放在 TransactionalEventListener
:
使用 ApplicationEventPublisher
发布 UserCreatedEvent
:
public class EmailServiceImpl {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Transactional(rollbackFor = Exception.class)
public User createUserAndSendEmail(UserDto userDto) throws UserEmailException {
try {
User user = userService.create(userDto);
//Publish UserCreatedEvent such the UserCreatedEventHandler can handled it after TX commit
applicationContext.publishEvent(new UserCreatedEvent(user));
return user;
} catch (Exception exception) {
throw new UserEmailException(exception.getMessage());
}
}
}
并且 UserCreatedEvent
将在 TX 提交后由此处理程序处理:
@Component
public class UserCreatedEventHandler {
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handle(UserCreatedEvent event) {
try{
amazonEmailService.sendMail(user);
System.out.println("Good, can send email.");
}catch(Exception exception){
System.out.println("Sad, fail to send email , so remove user from DB...");
userService.remove();
}
}
}
Deinum 抓得好。如果您使用我的建议,则必须将 userService.create()
更改为 @Transactional(propagation = Propagation.REQUIRES)
我想在数据库中创建一个用户,并使用 AWS SES 向这个案例的用户发送电子邮件。
- 如果用户在数据库中成功提交事务 => 发送电子邮件
- 如果发送电子邮件 AWS SES 失败(检查异常)=> 回滚数据库中的用户创建
- 如果用户在数据库中提交事务失败 => 不要使用 AWS 向用户发送电子邮件
我的代码有问题:如果我的 sendEmail
方法抛出异常,则会提交事务。
配置:Spring-使用spring-data-jpa
启动项目class EmailServiceImpl {
@Transactional(rollbackFor = Exception.class)
@Override
public User createUserAndSendEmail(UserDto userDto) throws UserEmailException {
try {
//rollback userCreation if sendEmail throw a checkedException
User user = userService.create(userDto);
sendEmail(user);
return user;
} catch (Exception exception) {
throw new UserEmailException(exception.getMessage());
}
}
//don't send email if userCommit in database failed
private void sendEmail(User user) {
amazonEmailService.sendMail(user);
}
}
class UserServiceImpl {
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public User create(UserDto userDto) throws Exception {
User user = mapp(userDto);
return userRepository.save(user);
}
}
要在 TX 提交后执行某些操作,您可以使用 @TransactionalEventListener
和 TransactionPhase.AFTER_COMMIT
(默认设置)。把你想做的动作放在 TransactionalEventListener
:
使用 ApplicationEventPublisher
发布 UserCreatedEvent
:
public class EmailServiceImpl {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@Transactional(rollbackFor = Exception.class)
public User createUserAndSendEmail(UserDto userDto) throws UserEmailException {
try {
User user = userService.create(userDto);
//Publish UserCreatedEvent such the UserCreatedEventHandler can handled it after TX commit
applicationContext.publishEvent(new UserCreatedEvent(user));
return user;
} catch (Exception exception) {
throw new UserEmailException(exception.getMessage());
}
}
}
并且 UserCreatedEvent
将在 TX 提交后由此处理程序处理:
@Component
public class UserCreatedEventHandler {
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handle(UserCreatedEvent event) {
try{
amazonEmailService.sendMail(user);
System.out.println("Good, can send email.");
}catch(Exception exception){
System.out.println("Sad, fail to send email , so remove user from DB...");
userService.remove();
}
}
}
Deinum 抓得好。如果您使用我的建议,则必须将 userService.create()
更改为 @Transactional(propagation = Propagation.REQUIRES)