Topic: Can i have a private variable in a stateless
Hey,
Can i have a private variable in a stateless session bean (Ejb3)?
Lets say that i want to implement a DAO as a stateless , so i have a CRUD functions.
create, find ...
all of them need to use entity manager (i cant use injection because i dont know the persistence unit name at run time)
So i think to have - private EntityManager em;
and a setEntityManager function.
after i obtain the stateless i will set the entity manager and operate the functions that i want to do.
is it ok to have private varibale?
as i know you can store a state on stateless , but this is exactly what the injection does.
Thank you
[680 byte] By [
AvihaiMara] at [2007-11-27 5:56:39]

# 1
Absolutely.The "Stateless" in Stateless Session Bean (SLSB) refers to the fact that there is no
*client-specific* state held in a SLSB. That means that an invocation on a SLSB reference can
be handled by *any* instance of the SLSB bean class in the container.
However, it's perfectly fine to hold instance state within the bean class that is independent of any
particular client but that might be needed during processing of a business method.Resources
like EntityManagers are a good example.
BTW, for the example you mention, it would be even easier to inject the EntityManager rather
than having it set by a caller. Just add the following to the Stateless Session bean class.
@PersistenceContext
private EntityManager em;
The container will automatically inject an EntityManager into each bean instance for you
before any business methods are called on the instance.
--ken
ksaksa at 2007-7-12 16:27:37 >

# 2
you said -
"That means that an invocation on a SLSB reference can
be handled by *any* instance of the SLSB bean class in the container"
But if this instance can serve any request i will have a problem, becuae suppose i have set a variable and this instance can serve another request it will use the value that i set.
i cant use injection because i dont want to use the default persistence unit name and i dont know at compile the name of the persistence unit.