Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

EJBeans and static variables

Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From MZ Notes:
An enterprise bean must not use read/write static fields. Using read-only static fields is allowed. Therefore, it is recommended that all static fields in the enterprise bean class be
declared as final.
This rule is required to ensure consistent runtime semantics because while some EJB containers may use a single JVM to execute all enterprise bean's instances, others may
distribute the instances across multiple JVMs.

I used static fields to read and write data into it inside the Stateless Session bean and it worked fine.
Does this mean aboove statements from MZ notes are incorrect? Am i missing something? Please exaplain ?
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

statement from MZ notes is absolutely correct (see core spec 21.1.2). Your observation only means that writing static variables works in your special situation (app server, bean code, and so on). But it isn't portable: If you switch to another app server you can't be sure that it works there too, or if, that it works in the same way.
[ November 26, 2008: Message edited by: Ralph Jaus ]
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using not final static field is not only it is not portable but also it is not distributable. that means your app in clustered environment will behave unpredictable manner. keep in mind that changing the value of a static field will effect only to the particular server but not to others in same cluster since static fields are not it shared memory.

actually I think sun should implement a proper way to come across this issue/requirement.something like application level attributes in servlet
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic