File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJBeans and static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJBeans and static variables" Watch "EJBeans and static variables" New topic

EJBeans and static variables

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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 ?

Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342

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 ]

SCJP 5 (98%) - SCBCD 5 (98%)
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 404
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
I agree. Here's the link:
subject: EJBeans and static variables
It's not a secret anymore!