*
The moose likes EJB and other Java EE Technologies and the fly likes static final variables initilization in EJBs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "static final variables initilization in EJBs" Watch "static final variables initilization in EJBs" New topic
Author

static final variables initilization in EJBs

Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
Hello All,
Does any one know a better solution to assign the values to static final variables in EJBs (Entity Beans and also Session Beans). We have a need to initilize some of the static final variables in each of the CMP and that has to be read from a resource such as a config file bundled with in the jar file. I tried initilize the final values with in the static block of code in EJB's but while the EJBs are getting deployed to the Container ( Oracle 9iAS OC4J container ) it throws an Exception such as resource not found Exception !!!
If I do the same thing with in a static block of code in other plan class (not the EJB's) they work fine and all the static final variables are initilized !!!
But I want the static final variables to be part of EJB's and initilize them when the class is getting loaded!!! becasue I don't want to use one more plan class just to hold my final variable values
Any help is appreciated very much
Thank you
Mallik
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
"Mallik HM",
I'm afraid I have to ask you to change your display name as our naming policy does not allow initials for last name.
Thanks.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
Does any one has a solution for this??
-Mallik
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I think you'll find that the EJB spec tells you you're not allowed to use statics, for precisely this kind of reason. Anything that multiple beans need to talk to should be located via JCA or JNDI or similar.


[Jess in Action][AskingGoodQuestions]
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
The specification also says that you can use static final variables... so thought make use of this for some of the Preferences to set up. It looks like the loading of the ejb classes is not in our hands (as they get compiled to a different wrapper classes and then they are loaded ). If some one has solved the similare problem please let me know.
I can make use of the env-entry variables, but that leads to JNDI lookups and I still think just the static final variables are good for this.
-- Mallik
Danl Thompson
Ranch Hand

Joined: Apr 04, 2001
Posts: 93
Mikal, I think the problem is the static initialization block. Clearly you can define a static final String inside an EJB. Nothing wrong with that. Trying to initialize it at class load time is the problem.
Try the alternative approach of putting final static strings that need initialization in the JNDI env namespace and do a lookup on them. Pretty much anytime you might think of something as a singleton or a static in EJB land it really belongs as a JNDI lookup.
dt
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
Thank you Danl,
Do you or some one know the best method of populated the JNDI env namespace? or suggest some pointers?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Mallik Hiremath:
Thank you Danl,
Do you or some one know the best method of populated the JNDI env namespace? or suggest some pointers?

I think Danl might have referred to the "private JNDI namespace", which is populated based on your deployment descriptor.
For example, instead of having private static final String MY_CONSTANT = "foo", you'd have

and
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
Thank you Lasse,
Yes, I know the env-entry tag, but every time I have to compare with some thing I need to lookup the JNDI, does any one is this an expensive statment?
Does any know how to specify constants that is applicable to all the EJBs in the application?
Thank you
Mallik
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Yes, the JNDI lookup is expensive compared to a static final but as the variable is read-only, you only need to look it up once at bean creation time etc.
Btw, sorry about not reading the thread thoroughly. I just realized that the env-entry was already out there...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static final variables initilization in EJBs
 
Similar Threads
How to deploy URLs like default URL provider in websphere
long post IBM.158
Pl Validate the points
Localization for SWINGS
Cleared SCBCD last week.. some notes