Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static final variables initilization in EJBs

 
Mallik Hiremath
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Mallik Hiremath
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does any one has a solution for this??
-Mallik
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mallik Hiremath
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic