gpt4 book ai didi

transactions - JMS 事务问题

转载 作者:行者123 更新时间:2023-12-04 03:57:31 25 4
gpt4 key购买 nike

我对 JMS 和事务有一个我不完全理解的问题。我的应用程序有一个 JDBC 资源和两个 JMS 队列。队列的消息生产者是在同一个 jms session 对象的同一个无状态 session bean 中创建的。我使用队列的方式如下: 创建一个实体并将其 id 作为属性保存在 JMS 消息中并发送到队列。实体的创建和向队列的提交发生在同一个事务中。我的消息驱动 bean 然后通过 JMS 消息中的 ID 从数据库中检索实体并对其进行处理。

代码大致如下:

public long doSomething(String message) {
SomeObject obj = new SomeObject(message);
entityManager.persist(obj)

// submit to JMS queue
try {
Message jmsMessage = session.createMessage();
jmsMessage.setLongProperty("id", obj.getId());
messageProducer.send(jmsMessage);
} catch (JMSException ex) {
Logger.getLogger(NotificationQueue.class.getName()).log(Level.SEVERE, null, ex);
}

return obj.getId();
}

MDB 的 onMessage 方法:
public void onMessage(Message message) {
Long id;
try {
id = message.getLongProperty("id");
} catch (Exception ex) {
Logger.getLogger(AlertMessageListener.class.getName()).log(Level.SEVERE, null, ex);
throw new EJBException(ex);
}

SomeObject obj = entityManager.find(SomeObject.class, id);
obj.process();
}

在 onMessage() 方法中从数据库中检索实体时,日志文件中出现以下异常:
FINE: ENTRY com.test.app.alert.control.AlertMessageListener onMessage
FINE: ENTRY com.test.app.alert.control.MessageDao find
FINER: client acquired: 2104888816
FINER: TX binding to tx mgr, status=STATUS_ACTIVE
FINER: acquire unit of work: 1368213481
FINEST: Execute query ReadObjectQuery(name="readObject" referenceClass=Message sql="SELECT ID, DTYPE, MESSAGE, REPORTTIME, SENDER_USERNAME, ALERTSTATE, TIMERHANDLE, CATEGORY_ID, PRIORITY_PRIOLEVEL FROM MESSAGE WHERE (ID = ?)")
SEVERE: prepareTransaction (XA) on JMSService:jmsdirect failed for connectionId:7979865462417759232 due to Unknown JMSService server error ERROR: com.sun.messaging.jmq.jmsserver.util.BrokerException: Bad transaction state transition. Cannot perform operation PREPARE_TRANSACTION(56) (XAFlag=null) on a transaction in state STARTED(1).
WARNING: JTS5031: Exception [java.lang.RuntimeException: javax.transaction.xa.XAException] on Resource [prepare] operation.
SEVERE: rollbackTransaction (XA) on JMSService:jmsdirect failed for connectionId:7979865462417759232:transactionId=7979865462479908608 due to Unknown JMSService server error ERROR: com.sun.messaging.jmq.jmsserver.util.BrokerException: Bad transaction state transition. Cannot perform operation ROLLBACK_TRANSACTION(48) (XAFlag=null) on a transaction in state STARTED(1).
WARNING: JTS5068: Unexpected error occurred in rollback
javax.transaction.xa.XAException
at com.sun.messaging.jms.ra.DirectXAResource.rollback(DirectXAResource.java:703)
at com.sun.jts.jta.TransactionState.rollback(TransactionState.java:193)
at com.sun.jts.jtsxa.OTSResourceImpl.rollback(OTSResourceImpl.java:333)
at com.sun.jts.CosTransactions.RegisteredResources.distributeRollback(RegisteredResources.java:1063)
at com.sun.jts.CosTransactions.TopCoordinator.rollback(TopCoordinator.java:2299)
at com.sun.jts.CosTransactions.CoordinatorTerm.commit(CoordinatorTerm.java:420)
at com.sun.jts.CosTransactions.TerminatorImpl.commit(TerminatorImpl.java:250)
at com.sun.jts.CosTransactions.CurrentImpl.commit(CurrentImpl.java:623)
at com.sun.jts.jta.TransactionManagerImpl.commit(TransactionManagerImpl.java:319)
at com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate.commitDistributedTransaction(JavaEETransactionManagerJTSDelegate.java:173)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:873)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5115)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4880)
at com.sun.ejb.containers.MessageBeanContainer.afterMessageDeliveryInternal(MessageBeanContainer.java:1207)
at com.sun.ejb.containers.MessageBeanContainer.afterMessageDelivery(MessageBeanContainer.java:1180)
at com.sun.ejb.containers.MessageBeanListenerImpl.afterMessageDelivery(MessageBeanListenerImpl.java:86)
at com.sun.enterprise.connectors.inbound.MessageEndpointInvocationHandler.invoke(MessageEndpointInvocationHandler.java:143)
at $Proxy260.afterDelivery(Unknown Source)
at com.sun.messaging.jms.ra.OnMessageRunner.run(OnMessageRunner.java:328)
at com.sun.enterprise.connectors.work.OneWork.doWork(OneWork.java:114)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:496)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:537)
Caused by: com.sun.messaging.jmq.jmsservice.JMSServiceException: rollbackTransaction: rollback transaction failed. Connection ID: 7979865462417759232, Transaction ID: 7979865462479908608, XID: null
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.rollbackTransaction(IMQDirectService.java:1827)
at com.sun.messaging.jms.ra.DirectXAResource.rollback(DirectXAResource.java:672)
... 21 more
Caused by: com.sun.messaging.jmq.jmsserver.util.BrokerException: Bad transaction state transition. Cannot perform operation ROLLBACK_TRANSACTION(48) (XAFlag=null) on a transaction in state STARTED(1).
at com.sun.messaging.jmq.jmsserver.data.TransactionState.nextState(TransactionState.java:449)
at com.sun.messaging.jmq.jmsserver.data.handlers.TransactionHandler.preRollback(TransactionHandler.java:1586)
at com.sun.messaging.jmq.jmsserver.data.protocol.ProtocolImpl.rollbackTransaction(ProtocolImpl.java:777)
at com.sun.messaging.jmq.jmsserver.service.imq.IMQDirectService.rollbackTransaction(IMQDirectService.java:1816)
... 22 more

FINER: TX afterCompletion callback, status=ROLLEDBACK
FINER: release unit of work
FINER: client released
FINEST: Register the existing object com.test.app.alert.entity.AlertMessage@7981d22
FINER: end unit of work commit
FINEST: Register the existing object Mailserver
FINEST: Register the existing object Low
FINEST: Register the existing object u0 u0 (u0)
FINEST: Register the existing object Sankt Augustin
FINE: RETURN com.test.app.alert.control.MessageDao find
...
WARNING: javax.ejb.EJBException
javax.ejb.EJBException: Transaction aborted
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5121)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4880)
at com.sun.ejb.containers.MessageBeanContainer.afterMessageDeliveryInternal(MessageBeanContainer.java:1207)
at com.sun.ejb.containers.MessageBeanContainer.afterMessageDelivery(MessageBeanContainer.java:1180)
at com.sun.ejb.containers.MessageBeanListenerImpl.afterMessageDelivery(MessageBeanListenerImpl.java:86)
at com.sun.enterprise.connectors.inbound.MessageEndpointInvocationHandler.invoke(MessageEndpointInvocationHandler.java:143)
at $Proxy260.afterDelivery(Unknown Source)
at com.sun.messaging.jms.ra.OnMessageRunner.run(OnMessageRunner.java:328)
at com.sun.enterprise.connectors.work.OneWork.doWork(OneWork.java:114)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:496)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:537)
Caused by: javax.transaction.RollbackException
at com.sun.jts.jta.TransactionManagerImpl.commit(TransactionManagerImpl.java:321)
at com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate.commitDistributedTransaction(JavaEETransactionManagerJTSDelegate.java:173)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:873)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5115)
... 10 more

以下信息在之前(创建实体之后)的日志文件中:
INFO: DXAR:start():Warning:Received diff Xid for open txnId:switching transactionId:
DXAR Xid=(GlobalTransactionID=[B@4eec2030, BranchQualifier=[B@703557ca)
DXAR TXid=7979865462479908608
got Xid=(GlobalTransactionID=[B@3e16c853, BranchQualifier=[B@49b9e0fd)
got TXid=7979865462480472064

这个异常究竟是什么意思:
Bad transaction state transition. Cannot perform operation ROLLBACK_TRANSACTION(48) (XAFlag=null) on a transaction in state STARTED(1).

我将 Glassfish v3.1-b35 与 JPA 2.0(和 Apache Derby)以及 GF 附带的标准 JMS 提供程序一起使用。事务设置以及 JMS 资源(嵌入式模式)都是默认设置。事务是容器管理的。任何想法这里出了什么问题?异常经常发生。

最佳答案

我已经就此事联系过 Oracle:他们建议不要在无状态 session bean 中缓存 JMS 连接。相反,连接应该按需获取并立即释放。这样做时没有性能开销,因为连接句柄是实际物理连接的薄包装器(请参阅 Java EE 连接器体系结构规范的第 6.4.3 节)。您也可以引用 glassfish 邮件列表中的 this thread 来解决类似问题。

关于transactions - JMS 事务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4618005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com