why nullify instance variables when destroying a stateful EJB?

303 Views Asked by At

In an example of the @remove annotation on a stateful EJB, the annotated method nullifys the bean's instance variables. Why? Surely destroying the bean destroys its contents, i.e. any variables?

Thanks, Jon

3

There are 3 best solutions below

0
On

Can you post the example source code? Or don't. Pro-actively setting null is not needed - when EJB is destroyed and garbage collected soon later, all objects it references (of course providing there are no other references to them) will be garbage collected as well.

2
On

Setting all fields of an object to null has two useful effects:

  • It provides a hard barrier against logic errors that would lead to an invalid object being reused. The application would crash instead of silently producing incorrect results.

  • It helps the Java VM garbage collector by removing edges from the object reference graph, thus improving the overall performance.

0
On

If ejbRemove(), Attributes of instance are wiped clean and client still has reference to instance. Client still can access the same object. That is not desirable.