The moose likes EJB and Other Java EE Technologies and the fly likes EJB instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » EJB and Other Java EE Technologies
Reply Bookmark "EJB instance variables" Watch "EJB instance variables" New topic
Author

EJB instance variables

Paul Hunnisett
Greenhorn

Joined: Mar 18, 2010
Posts: 5
Is it safe to use instance variables in EJB - stateless or otherwise? When beans are returned to the pools do their properties get 'reset'? Is there a lifecycle method I could use for this purpose if not?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 23635

Paul,
Welcome to JavaRanch!

It depends on what is in the instance variables. If it is immutable/references to classes without interfaces (for mocking), it is fine. If it is data you rely on, it is dangerous because you don't know what state it will be in later.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3, OCAJP
Paul Hunnisett
Greenhorn

Joined: Mar 18, 2010
Posts: 5
Thanks - that's pretty much what I thought. Which makes me wonder what the point of the @EJB annotation is... As far as Ican see it can only be used for variables at instance level in either EJBs or Servlets; which, in both cases, is dangerous...

What am I missing here?
Paul Hunnisett
Greenhorn

Joined: Mar 18, 2010
Posts: 5
http://java.sun.com/javaee/5/docs/tutorial/doc/bnbly.html

Leads me to believe that it should be safe to use instance variables in a Stateful EJB...
Krum Bakalsky
Ranch Hand

Joined: Mar 14, 2010
Posts: 46
You should introduce yourself with the life cycle of stateless/stateful bean instances.

For stateless beans:
Using directly instance variables is risky, since after business method execution, the bean instance goes back to the pool, and the EJB container is not required to keep its state in the sense that the next business method execution of the same bean class could be served by a totally different instance! As a rule, you should never rely on such state!

Using instance variables that are resource references (@EJB, @Resource, @PersistenceContext, @PersistenceUnit) is safe, since before a business method execution takes place, the EJB container takes the care to resolve those references on the chosen instance.


SCJP 6 (86% - the hard way), SCBCD 5 (81% - the hard way)
Paul Hunnisett
Greenhorn

Joined: Mar 18, 2010
Posts: 5
Krum Bakalsky wrote:
Using instance variables that are resource references (@EJB, @Resource, @PersistenceContext, @PersistenceUnit) is safe, since before a business method execution takes place, the EJB container takes the care to resolve those references on the chosen instance.


Thank you! That's what I was hoping was the case. I spent ages googling the lifecycle of EJBs but found limited stuff and none that answered my questions about annotations.
 
 
subject: EJB instance variables
 
Threads others viewed
class variables
is field or property based annotation same
are instance variables thread safe ?
By convention - are there for exemple first variables, second getters and setters and third methods?
Rules for Constructors
IntelliJ Java IDE