• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB variable intialization

 
chandra kommalapati
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Sahil Dave
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
Nico Van Brandt
Ranch Hand
Posts: 70
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • 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.
 
Victor Herrera
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.




 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!