交易并发送电子邮件 [英] Transaction and sending an email

查看:176
本文介绍了交易并发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到用户在Web应用程序上创建新帐户的常见用例,以及向用户地址发送确认电子邮件的应用程序。从我所看到的,这通常以三种方式之一实现:


  1. Web控制器调用一个服务方法,创建用户帐户,并在单个事务中发送电子邮件。

  2. Web控制器调用一个服务方法(使用tx propagation = never),它调用自己的第一种方法来创建用户帐户,然后调用第二种方法来发送电子邮件。

  3. Web控制器调用第一种服务方式,在事务中创建用户帐户,然后是第二种方法发送电子邮件的服务方法。

第一种方法简单直观,但存在交易回滚的风险电子邮件已发送,从而使电子邮件无效。第二种方法更复杂,但它保证只有在用户创建实际成功时才发送电子邮件。第三种方法很简单,但是负责网络层的业务逻辑,它不应该需要知道。



没有一种更简单的方法,也许AOP驱动,这确保只有在用户创建事务实际成功时才会发送电子邮件?我认为第一种方法可能会失败是偏执的吗?



我们正在使用Java EE + Spring堆栈,并愿意集成其他API(AOP?Spring Integration? )实现这一点。



干杯!

解决方案

我目前正在用来解决这个问题:



http://download.oracle.com/javaee/6/api/javax/transaction/Synchronization.html


Considering the common use case of a user creating a new account on a web application and the application sending a confirmation email to the user's address. From what I've seen, this is typically implemented in one of 3 ways:

  1. The web controller calls a service method, which creates the user account and sends the email, both within a single transaction.
  2. The web controller calls a service method (with tx propagation = never), which invokes a 1st method on itself to create the user account within a transaction, and then invokes a 2nd method on itself to send the email.
  3. The web controller calls a 1st service method, which creates the user account within a transaction, then a 2nd service method that sends the email.

The 1st approach is simple and straightforward, but there is a risk that the transaction is rolled back after the email was sent, thereby making the email invalid. The 2nd approach is more complicated, but it guarantees that the email is sent only if user creation has actually succeeded. The 3rd approach is simple but burdens the web layer with business logic that it shouldn't need to know.

Isn't there a simpler approach, maybe AOP-driven, that guarantees that the email will be sent only if the user creation transaction actually succeeded? Am I paranoid in thinking that the 1st approach could fail?

We're using a Java EE + Spring stack and are willing to integrate additional APIs (AOP? Spring Integration?) to achieve this.

Cheers!

解决方案

Another option I am currently using to solve this problem:

http://download.oracle.com/javaee/6/api/javax/transaction/Synchronization.html

这篇关于交易并发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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