• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Question about writeable static fields in EJB 3.1

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Creator of Enthuware JWS+ V6
Posts: 3398
312
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic