• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does transaction isolation applies on static variables ?

 
Amr k. Saleh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
venk somis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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;
}
}



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic