Should I declare the entered variables as temporary in Java EE?

Should I declare the entered variables as transient in Java EE?

I get a FindBugs warning:

The com.playaround.HelloServlet class defines a non-transition non-serializable acceleration instance field Unit
This Serializable class defines a non-primitive instance field that is not temporary, Serializable or java.lang.Object and does not implement the Externalizable interface or the readObject () and writeObject () methods.
Objects of this class will not be deserialized correctly if an object without Serializable is stored in this field.

Used code:

 /** * Sample of a web service reference. */ @WebServiceRef private AccelerationUnit accelerationUnit; 

The same question applies to @Resource , @Inject , @PersistenceUnit , etc.

+6
source share
2 answers

It depends;) With @Inject and other CDI annotations, you should check chapter 6.6 of the JSR-299 specification . You have information in which beans are "passivable."

About a session of standless and singleton beans, according to the EJB specification, they cannot be serialized (since passivation of ejb does not occur)

Think, and the most problematic is the beans state session. All JavaEE resources (EJB, InitialContext, SessionContext, etc.) will be restored after activation, but you must take care of other non-serializable fields and open connections. Thus, in your IMHO case, you should mark the Unit acceleration as transient and restore it in the ejbActivate event or enable CDI and creation methods to automatically enter the field.

+7
source

In the AccelerationUnit class definition, do this:

 class AccelerationUnit implements Serializable{ // your code here } 
-3
source

Source: https://habr.com/ru/post/954427/


All Articles