• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about writeable static fields in EJB 3.1

 
Ajay Dhar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is not conform to the spec and there is no way to predict the value in a distributed environment.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2264
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic