简体   繁体   English

Bean托管的MDB和数据库异常

[英]Bean Managed MDB and Database exceptions

I have a Bean managed MDB -InvoiceInquiryMessageBean with the following definition which calls a CMT - InvoiceManager which performs database operations. 我有一个具有以下定义的Bean管理的MDB -InvoiceInquiryMessageBean ,该定义调用CMT-执行数据库操作的InvoiceManager

The MDB is explicitly mentioned as Bean managed and the onMessage() has a transaction NOT_SUPPORTED . MDB被明确提到为Bean管理 ,并且onMessage()具有事务NOT_SUPPORTED Therefore this MDB runs without a transaction demarcation. 因此,该MDB无需事务划分即可运行。

The InvoiceManager bean below has no transaction type or transaction attribute defined. 下面的InvoiceManager bean没有定义交易类型或交易属性。 so by default its a container managed CMT and has a transaction type of REQUIRED by default.This bean performs database operations. 因此,默认情况下,它是容器管理的CMT ,默认情况下的事务类型为REQUIRED 。此bean执行数据库操作。 The questions is 问题是

Question #1 问题1

If there are any errors/exceptions while performing the DB operations like(Primary key violated, DB deadlock like SQL server Error code 1205), the DB transaction is considered failed. 如果在执行诸如(违反主键,SQL Server错误代码1205的数据库死锁)之类的数据库操作时出现任何错误/异常,则认为该数据库事务失败。 Will this DB transaction failure impact the calling MDB. 此数据库事务故障是否会影响调用MDB。

The reason for this question is i see the messages getting redelivered to the MDB sometimes during database exceptions. 出现此问题的原因是,我有时在数据库异常期间看到消息重新传递到MDB。 Even though the MDB is defined not to participate in any container managed transaction, the db issue is related to the database transaction and will this cause the message to be redelivered to the MDB. 即使MDB被定义为不参与任何容器管理的事务,但是db问题与数据库事务有关,并且这将导致消息重新传递到MDB。

Please if my question is not clear let me know. 如果我的问题不清楚,请告诉我。

@TransactionManagement(TransactionManagementType.BEAN)
@MessageDriven(name = "NonPersistentInquiryMessageBean", activationConfig = {      @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")    })
public class InvoiceInquiryMessageBean implements MessageListener
{
      @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
      public void onMessage(Message msg)
      {
          call a CMT Bean(CMT_DB_Bean) which performs database operations
      }
}

CMT  Bean

@Stateless
public class InvoiceManager implements InvoiceManager Local {
entityManager.update();

}  

The reason for this question is i see the messages getting redelivered to the MDB sometimes during database exceptions. 出现此问题的原因是,我有时在数据库异常期间看到消息重新传递到MDB。

I think your issue is about exception handling, it is not about transaction demarcation. 我认为您的问题与异常处理有关,而与事务划分无关。 The fact that both, the MDB and session bean execute in different transactions doesn't means that they are isolated from exception propagation. MDB和会话Bean都在不同的事务中执行的事实并不意味着它们与异常传播是隔离的。

When your session bean throws an exception, it propagates to the Client (in this case InvoiceInquiryMessageBean). 当会话bean引发异常时,它将传播到客户端(在本例中为InvoiceInquiryMessageBean)。 If your MDB doesn't handle it the Container will not acknowledge the message, therefore, it will be redelivered. 如果您的MDB不处理它,那么容器将不会确认该消息,因此将重新发送该消息。

One possible solution is to handle the exception in the MDB. 一种可能的解决方案是在MDB中处理异常。

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  public void onMessage(Message msg)
  {
try { 
      call a CMT Bean(CMT_DB_Bean) which performs database operations
} catch (Exception e) {
 //log  
}
  }

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

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