交易并发送电子邮件

编程入门 行业动态 更新时间:2024-10-10 04:27:42
本文介绍了交易并发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 考虑到用户在Web应用程序上创建新帐户的常见用例,以及向用户地址发送确认电子邮件的应用程序。从我所看到的,这通常以三种方式之一实现:

  • Web控制器调用一个服务方法,创建用户帐户,并在单个事务中发送电子邮件。
  • Web控制器调用一个服务方法(使用tx propagation = never),它调用自己的第一种方法来创建用户帐户,然后调用第二种方法来发送电子邮件。
  • Web控制器调用第一种服务方式,在事务中创建用户帐户,然后是第二种方法发送电子邮件的服务方法。
  • 第一种方法简单直观,但存在交易回滚的风险电子邮件已发送,从而使电子邮件无效。第二种方法更复杂,但它保证只有在用户创建实际成功时才发送电子邮件。第三种方法很简单,但是负责网络层的业务逻辑,它不应该需要知道。

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

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

    干杯!

    解决方案

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

    download.oracle/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:

  • The web controller calls a service method, which creates the user account and sends the email, both within a single transaction.
  • 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.
  • 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:

    download.oracle/javaee/6/api/javax/transaction/Synchronization.html

    更多推荐

    交易并发送电子邮件

    本文发布于:2023-10-12 16:14:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1485239.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:发送电子邮件

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!