wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Question about writeable static fields in EJB 3.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Question about writeable static fields in EJB 3.1" Watch "Question about writeable static fields in EJB 3.1" New topic
Author

Question about writeable static fields in EJB 3.1

Ajay Dhar
Ranch Hand

Joined: Jan 26, 2011
Posts: 30
This is a question regarding code in the first example in Ivan Krizsan's OCP Java EE 6 EJB Developer Study Notes on page 22. But first on page 13 of the study notes, and in the EJB 3.1 specification (chapter 21, section 2), it states that one EJB programming restriction is that an EJB must not use writable static fields. The motivation being to ensure consistency in a distributed environment.

Here is the code of a stateful session bean taken from the study notes on page 22:



If you look at line number 26, there is a static int declared as an instance variable. This static variable is updated in the @PostConstruct method. Doesn't this violate the restriction that an EJB must not use writeable static fields?

Thanks,
Ajay


OCPJP 6, OCEEJBD 6, GIAC Secure Software Programmer-Java (GSSP-Java)
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 542
    
    4

Yes, it is not conform to the spec and there is no way to predict the value in a distributed environment.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1662
    
  25

Yes, the examples that are handled by Ivan are just for testing purposes. In a distributed production environment these examples should not be used.

I guess an extra note explaining this exception would be helpful.

Regards,
Frits
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hello!
The simplest way that I could think of to keep track of the number of instances of the session bean was a writeable static field, as seen in the example.
This was done in order to show when a certain instance of the session bean is instantiated and destroyed and how many instances of the session bean are created.
There is nothing in the instructions that suggests using the example in a distributed environment.
I have included a comment on the reason for using a writable static field in the latest version of the study notes.
Thanks for bringing this to my attention!
Best wishes!
Ivan


My free books and tutorials: http://www.slideshare.net/krizsan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about writeable static fields in EJB 3.1