我有以下情况:
- 客户端正在调用无状态本地 EJB - 托管事务从此调用开始
- 本地 EJB 构建 InitialContext 并查找远程 EJB
- 本地 EJB 调用远程 EJB 上的方法
- 本地 EJB 关闭上下文和与远程 EJB 的连接
- 容器尝试提交事务
无法提交分布式事务,因为无法联系到参与事务的远程 EJB 的连接,因为与它的连接已关闭。
我的问题是:当事务已经处于活动状态时,是否可以使用远程 EJB 调用?我应该如何关闭用于查找远程 EJB 的上下文?
以下伪代码说明了我的问题:
@Stateless
public class LocalEjb {
public void localEJBMethod() {
//transaction starts before this method execution
Context ctx = //create initial context
RemoteEjb remoteEjb = (RemoteEjb) ctx.lookup("jndi name");
remoteEjb.remoteMethod(); //remote EJB takes part in distributed transaction
ctx.close();
//error occurrs when container tries to commit distributed transaction after
//this method returns
}
}
public class ClientClass { //a CDI component, for example
@EJB
private LocalEjb localEjb;
public void clientMethod() {
localEjb.localEjbMethod();
}
}