• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing Stateless Session Bean's member variable  RSS feed

 
Sec Onda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

Suppose I have a stateless bean like the following:

public class HitCounter implements SessionBean{
int hitCount = 0; // member variable

public void hit() {
hitCount++;
}
.....
}

Is there any way to guarantee hitCount is incremented by 2 if 2 clients called hit() concurrently?

As far as I know, synchronized method is not allowed in EJB, and most importantly, there may be actually two separate instances serving the clients. So, this makes me feel synchronizing the member variable is impossible.

Does anyone have idea? Or we should actually keep the synchronized data outside the EJB? Thank you very much.

S. Ond
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sec,


Is there any way to guarantee hitCount is incremented by 2 if 2 clients called hit() concurrently?

No it�s not. Basically the container will always get an instance from the pool, will increment the counter and will return the instance to the pool. The counter will always have only two values: 0 and 1, no mater how many users call the hit() method.

As far as I know, synchronized method is not allowed in EJB, and most importantly, there may be actually two separate instances serving the clients. So, this makes me feel synchronizing the member variable is impossible.

That�s very true, there is no need to synchronize member instance variables with ejbs. However the issue here is that SLSB are stateless and therefore they cannot have/retain any state.
Regards.
 
Sec Onda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No it�s not. Basically the container will always get an instance from the pool, will increment the counter and will return the instance to the pool. The counter will always have only two values: 0 and 1, no mater how many users call the hit() method.


Is it always true that the counter only has two values 0 and 1? I did try to deploy such an EJB. The counter would become 2 when I called the client 2 times.


That�s very true, there is no need to synchronize member instance variables with ejbs. However the issue here is that SLSB are stateless and therefore they cannot have/retain any state.


What I understand is, a stateless bean can still have state, but the state is not particularly for a specific client. Is my understanding right?

Regards,

S. Onda
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What I understand is, a stateless bean can still have state, but the state is not particularly for a specific client. Is my understanding right?

Yes that�s true. For some reason I got this silly idea that the container will reset the member variables after returning the instance to the pool. But you just proved that I was wrong.
This however leaves you with only one choice in order to �synchronize� the count member variable: force the container to create only one instance of this bean. If you set the initial number of instances in pool = max number of insatances in pool = 1 you�ll have a unique instance hit by all clients and the container will synchronize the data access for you.
Regards.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!