| Author |
Does transaction isolation applies on static variables ?
|
Amr k. Saleh
Greenhorn
Joined: Aug 23, 2010
Posts: 26
|
|
Hi All,
it might be a dummy question, does transaction isolation applies on static variables? i mean does EJB container manages locking static variable if used in a transaction ?
thanks in advance
|
 |
s patnaik
Ranch Hand
Joined: Jul 17, 2011
Posts: 45
|
|
I am not sure if I understand your question correctly.
What I can say is that the EJB container only makes sure that a bean instance is associated with only one thread at a time.
A static field can still be accessible across all threads. So thread A (associated with EJB instance A) can start a transaction and access and modify the static field. Simultaneously thread B (associated with EJB instance B) can start another transaction and access and modify the same static field at the same time.
Hope that helps !
|
SCJP 5, OCPJP7, OCMJEA 5
|
 |
venk somis
Greenhorn
Joined: Jul 02, 2012
Posts: 2
|
|
May not understand your question properly, why someone wants to use a Static variable in Ejb ? You want have that in Stateless bean ? What is the purpose , One of the very important features that EJB-Container
provided is Thread-safety. So if you use Static in your EJBs, those are not Thread Safe.
One more thing, if you are looking for having some Singleton Classes in EJB layer, you can have that with latest EJB 3.1
The following bean load data in init() method and saves the data when destroyed
@Singleton
public class YourTestBean {
@PersistenceContext
private EntityManager entityManager;
private Rate rate;
@PostConstruct
private void init() {
rate = entityManager.find(Rate.class, 1);
}
@PreDestroy
private void destroy() {
entityManager.merge(rate);
}
public void setRate(Rate rate) {
this.rate = rate;
}
public Rate getRate() {
return rate;
}
}
|
 |
 |
|
|
subject: Does transaction isolation applies on static variables ?
|
|
|