简体   繁体   English

EJB,XA事务和错误处理

[英]EJBs, XA transactions and error handling

We have what we believe is a fairly common XA use case: 我们认为这是一个相当常见的XA用例:

  1. read a message from an in-queue 从队列中读取消息
  2. write some data to a database 将一些数据写入数据库
  3. write a response message to an out-queue (which is different from the in-queue) 将响应消息写入出队列(与队列不同)

However we also need a way to handle internal errors and poison messages. 但是,我们还需要一种方法来处理内部错误和有害消息。 The control flow we have in mind is something like this: 我们想到的控制流程是这样的:

  1. read the message from the in-queue 从队列中读取消息
  2. write to the database 写入数据库
    • if there's an exception roll back the database transaction 如果有异常回滚数据库事务
    • if there's no exception run commit phase1 on the database 如果没有异常,则在数据库上运行commit phase1
  3. if everything went fine (no rollback and commit phase1 ok) write a success message to the out-queue 如果一切顺利(没有回滚并且提交phase1确定)将成功消息写入出队列
  4. if either commit phase1 on the database failed or there was an exception and the database transaction was rolled back write a failure message to the out-queue 如果数据库上的提交阶段1失败或者存在异常并且数据库事务已回滚,则将失败消息写入外部队列
  5. commit the in-queue, the out-queue and the database (unless rolled by because of an exception) 提交队列,队列和数据库(除非由于异常而滚动)

Is this a good approach, should we do it differently? 这是一个很好的方法,我们应该采取不同的方式吗? How can we do this with EJBs? 我们如何使用EJB执行此操作?

We're using EJB 3.1 on JBoss AS 7.2 / EAP 6.1, coding against Narayana directly is an option. 我们在JBoss AS 7.2 / EAP 6.1上使用EJB 3.1,直接对Narayana进行编码是一种选择。 The JDBC driver is ojdbc7-12.1.0.1 and the JMS RAR is MQ Series (don't know the version). JDBC驱动程序是ojdbc7-12.1.0.1,JMS RAR是MQ Series(不知道版本)。

What you could do is to use the Java EE event mechanism to get a notification when your transaction fails and create a subsequent output message. 您可以做的是使用Java EE事件机制在事务失败时获取通知并创建后续输出消息。

See 看到

You need to use a new transaction to write to the out queue to avoid rolling back the message writing as well. 您需要使用新事务写入out队列,以避免回滚写入消息。

You will still have the message in the input queue which caused the exception, since the rollback will prevent the successfull consumption. 您仍然会在输入队列中显示导致异常的消息,因为回滚将阻止成功使用。 You need to handle that separately, for example by the JMS provider. 您需要单独处理它,例如由JMS提供程序处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM