更改应用程序使其具有 WebSphere“特定版本”的 setRollbackOnly 行为
使用此任务以允许 EJB 3.0 之后版本的应用程序展示 EJB 3.0 之前版本的行为,并允许 EJB 3.0 之前版本的应用程序展示 EJB 3.0 之后版本的行为。本任务中提供此处理行为的步骤基于非常具体的处理方案,“关于此任务”部分中进行了说明。
关于此任务
处理方案:此任务的基础是以下处理方案。
系统会调用 EJB 方法以启动全局事务。在执行此方法时,系统会调用另一 EJB 方法以继续在同一事务内运行。在执行此方法时,系统会调用 setRollbackOnly() 方法。遇到此方案时的 EJB 容器行为按 EJB 规范执行。但是,在 WebSphere® Application Server 中,需要更改此行为以获取 EJB 3.0 支持。
JSR 220: Enterprise JavaBeans™,Version 3.0 EJB Core Contracts and Requirements 文档的 13.6.2.8 部分及先前版本的 EJB 规范指示:
“If the container initiated the transaction immediately before dispatching the business method to the instance (as opposed to the transaction being inherited from the caller), the container must note that the instance has invoked the setRollbackOnly method. When the business method invocation completes, the container must roll back rather than commit the transaction. If the business method has returned normally or with an application exception, the container must pass the method result or the application exception to the client after the container performed the rollback.” 而 14.3.11 部分中指示:“However, the container should not throw the javax.ejb.EJBException or java.rmi.RemoteException or if the container performs a transaction rollback because the instance has invoked the setRollbackOnly method on its EJBContext object. In this case, the container must rollback the transaction and pass the business method result or the application exception thrown by the business method to the client.”
从历史上看,WebSphere Application Server 已解释规范的以上部分以指示仅当事务在开始该事务的方法内标记为 RollbackOnly 时,才应应用 setRollbackOnly 行为。但是,对应 EJB 3.0 规范的 Compatibility Test Suite 要求标记为 RollbackOnly 的事务展示以上 setRollbackOnly 行为,不管该事务是否在开始该事务的方法内或从原始 EJB 方法调用的同一事务中的另一方法内标记为 RollbackOnly。
- 应用程序对带有容器管理的事务支持 (TX_REQUIRED) 的 EJB A 调用方法 art。
- 容器开始事务并调用该方法。
- 方法 A.art() 对带有容器管理的事务支持 (TX_REQUIRED) 的 EJB B 调用方法 bob。
- 在 B.bob() 内,系统对事务调用 setRollbackOnly 方法并完成。
EJB 3.0 之前的行为:因为未使用 B.bob() 方法启动事务,所以系统会向 A.art() 抛出 TranactionRolledbackException 并最终发送至客户机应用程序。
EJB 3.0 及之后版本中的行为:方法 B.bob() 正常返回至方法 A.art()。方法 A.art() 对事务执行回滚并将结果返回至客户机而不向客户机应用程序抛出异常(如 EJB 规范所述)。
- com.ibm.websphere.ejbcontainer.limitSetRollbackOnlyBehaviorToInstanceFor:此属性允许用户指定应用程序名称,在这些应用程序中,他们希望 EJB 3.0 模块中的 EJB 演示此部分中描述的 EJB 3.0 之前版本的 setRollbackOnly 行为。
- com.ibm.websphere.ejbcontainer.extendSetRollbackOnlyBehaviorBeyondInstanceFor:此属性允许用户指定应用程序名称,在这些应用程序中,他们希望 pre-EJB 3.0 模块中的 EJB 演示此部分中描述的 EJB 3.0 版本的 setRollbackOnly 行为。