EJB未在托管Bean中序列化 [英] EJB not serialized in Managed Bean

查看:128
本文介绍了EJB未在托管Bean中序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序服务器是WebSphere Application Server V8.我有一个会话范围内的托管bean,其中使用@EJB注释注入了EJB(EJB 3.0). EJB是无状态的.

My application server is WebSphere Application Server V8. I have a session scoped managed bean in which I have injected EJB (EJB 3.0) using @EJB annotation. EJB is stateless.

   @ManagedBean
   @SessionScoped
    public class MyBean extends BaseBackingBean implements
    Serializable {

@EJB
private IDetails custInfo;

我正在分析会话数据并注意到NotSerializableException

I was analyzing the session data and noticed NotSerializableException

java.io.NotSerializableException: com.ejb.EJSLocal0SLDetailsImpl_081f812d at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1537) at
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1502) at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1420) at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) at 

现在,我尝试将EJB标记为瞬态,并且可以正常工作而不会引发NotSerializableException异常.

Now I tried to mark the EJB as transient and it works fine without throwing NotSerializableException exception.

@EJB
private transient IDetails custInfo;

这是正确的实现方式还是替代解决方案?

Is this correct implementation or what can be alternate solution?

我已引用 EJB是否应该作为实例变量并在JSF Managed Beans中标记为瞬态?,其中提到不需要将EJB标记为瞬态.那怎么可能出问题了?

I have referred Should EJBs be instance variables and marked as transient in JSF Managed Beans? where it is mentioned that marking EJB as transient is not required; then what can be wrong?

推荐答案

在具有本地和远程接口的WAS V8上实现了POC代码,并指出以下内容:

Implemented a POC code on WAS V8 with both Local and Remote interfaces, noted the following:

a.具有本地接口(EJB不实现Serializable)

// Initializing the EJB in the servlet
SerializableTestEJBLocal localSrvlt=new SerializableTestEJB(); 
//Try to serialize
FileOutputStream objFOS = new FileOutputStream("D:\\MYTEST\\testsrv.txt");
ObjectOutputStream objOpStr = new ObjectOutputStream(objFOS);
objOpStr.writeObject(localSrvlt);

这导致 java.io.NotSerializableException:com.ibm.test.SerializableTestEJB 在java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:.. 为了防止这种情况,EJB必须显式实现Serializable.

This resulted in java.io.NotSerializableException: com.ibm.test.SerializableTestEJB at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:.. To prevent this, the EJB had to explicitly implement Serializable.

b.使用远程接口(EJB不能实现可序列化)

//Obtain Remote Stub. 
SerializableTestEJBRemote seremoteSrvlt=(SerializableTestEJBRemote)PortableRemoteObject.narrow(homeObject, SerializableTestEJBRemote.class);

//Try serialization
FileOutputStream objFOS = new FileOutputStream("D:\\MYTEST\\testsrv.txt");
ObjectOutputStream objOpStr = new ObjectOutputStream(objFOS); 
objOpStr.writeObject(seremoteSrvlt);

序列化成功.

结论:

远程接口的固有机制是获取存根或代理,以允许使用此代理模式进行客户端-服务器通信.这涉及数据的编组和解组,因此proxy-stub在默认情况下是可序列化的,因此EJB不需要实现Serializable接口.

The inherent mechanism of remote interface is to obtain a stub or proxy to allow for client-server communication occurs using this proxy pattern. This involves marshalling and unmarshalling of data and hence the proxy-stub is Serializable by default and hence the EJB does not need to implement the Serializable interface.

但是本地接口不涉及远程查找和存根处理程序. EJB初始化类似于初始化本地可用的类,因此默认情况下序列化不可用.在这种情况下,EJB将需要实现可序列化的接口,或者需要将对象声明为瞬态以跳过序列化.

But the local interfaces does not involve remote look ups and stub handlers. EJB initializes is similar to initializing a locally available class, hence serialization is not available by default. In this scenario, either the EJB will need to implement serializable interface or the object will need to be declared transient to skip serialization.

我将变量声明为瞬态.这可能是特定于WebSphere的解决方案

I am declaring the variable as transient. This might be WebSphere specific solution

这篇关于EJB未在托管Bean中序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆