Reputation: 359
i use it this design pattern in j2ee :
public class myClass{
private static Hashtable<Integer, ContentClass> contents = new Hashtable<Integer, ContentClass>();
public synchronized static void m1(){
//some work on contents
}
public synchronized static void m2(){
//another some work on contents
}
I want to deploy this in 3 server that run glassfish
.
Quiz 1 : is it guarantee to be thread safe?
Quiz 2 : if the class change to Stateful EJB what happened? is it thread safe?
Upvotes: 0
Views: 179
Reputation: 4819
If you want to share a bean between three servers that run glashfish, you need to use EJB persistence with entity beans, not use stateless nor stateful session beans. But EJB persistence will not work with static members.
Thus, you should write:
public class myClass{
private Hashtable<Integer, ContentClass> contents = new Hashtable<Integer, ContentClass>();
public synchronized void m1(){
//some work on contents
}
public synchronized void m2(){
//another some work on contents
}
and add annotations or other means to explain the container how to persist contents
with some kind of mapping.
This way, you can persist this entity bean to a shared data source, and the three servers will be able to access it correctly. So, you need to define a data source, a persistence unit and an entity manager to manage this bean's instances.
This way, your code will be thread-safe. Accessing contents
will be correctly done.
Upvotes: 1