• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJB variable intialization

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear ranchers,

I am new to EJB.I need some help in designing the code.

1. I have some variables which should be used across the business methods.
So can i declare them as static and initialize them in a static block (like we use in normal java program).

But one of the posts here mentioned that , Its not suggestible to use static (R/W) variables .Only read only static variables should be declared as "static final".
Then in that case if i declare my variables as normal class variables how can i initialize them. (ejbCreate() ..?)

And i am not sure how to use ejbCreate() method in EJB 3.0.Could you please mention the syntax.

2. As per my logic the At the time of initialization it self I have to call a database function to fetch one initial value and store in of the variables.
Could you mention me where to call this class (which calls the data base function and fetch the new value) in EJB after initialization.

All the logic i have implemented in JAVA.
Please give me some inputs

Thanks in advance.

Regards,
chandra
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For you 1st query, you could create a Final class containing all the static final constants you want to use across classes/methods and import the class to use them.

For you second query, it depends on the type of enterprise bean you want. E.g for a Message Driven Bean, the method call would come inside a onMessage() method.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot really answer question one since I'm also fairly new to EJB and unfamiliar with the best practices.

To answer question two, I think you are refering to the Bean's lifecycle triggers?
You should try adding the @PostConstruct annotation to a method so this method gets calles when the bean gets created.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.


1.- You could use static variable if they will never gonna change while executing.

For example:



But if you need instance variables you could initializate them in @PostConstruct callback that you could also use to fetch the values from databases.




 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a class containing the attributes you need to maintain and define public getter-and-setter methods for each of the attribute. Include this class as a private member of your EJB class and in the ejbCreate method instantiate an object of the class. Depending on the type of EJB, the appropriate and frequently called life-cycle method will be invoked, wherein you can use the getter-or-setter methods of the class appropriately.
Note: Please study EJB3.0 well, before implementing them in LIVE projects.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic