• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a way to "synchronize" a stateless session bean's business method?

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is it possible to make it so that a stateless session bean's method is never executed concurrently by multiple users? I need a behaviour similar to synchronization of the method - if two users call the method at the same time, one of them waits in a queue for the other to finish. Of course the difference is that I want this behavior to be across all instances of the bean, not just the single instance that synchronization would provide.

Thank you,
Yuriy
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make all instances synchronize on a common shared object, perhaps a static variable that is used only for that purpose or a synchronized static method that synchronizes on the class.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you wish to prevent the same method from being invoked by concurrent clients?
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make all instances synchronize on a common shared object, perhaps a static variable that is used only for that purpose or a synchronized static method that synchronizes on the class.

I was under the impression that EJB code was supposed to avoid both synchronized blocks/methods and static variables (unless the members are treated as read-only constants.) The reasoning being that in a distributed environment it would fall apart.

Why would you wish to prevent the same method from being invoked by concurrent clients?

The method increments a value in the database by a constant, and the method return value is based on that. Two clients concurrently accessing the method must never get the same value. If there was a way to get a lock on database row for writing before the read, that would work too.

Thank you,
Yuriy
[ September 20, 2005: Message edited by: Yuriy Zilbergleyt ]
 
Valentin Tanase
Ranch Hand
Posts: 704
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yuryi,


Is it possible to make it so that a stateless session bean's method is never executed concurrently by multiple users? I need a behaviour similar to synchronization of the method

Synchronizing EJB instances is like trying to fly a plane backwards :-) and probably EJBs is not the right solution to your problem. Do you have the freedom to purpose another solution?
Regards.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin,

The "increment and update" code doesn't have to be inside an EJB, but it should be able to be called from an EJB. We are planning to use a stateless session bean to load and store client configuration data. The storing will often require the creation of new table rows with generated unique ids. The question is how to do this generation. I was thinking of using the UniqueIDEJB (http://jdj.sys-con.com/read/36169.htm) pattern, which uses another stateless session bean just for the "atomic" read-and-update of the current max id in the databases.

Thank you,
Yuriy
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two clients may try and access the DB concurrently, but the EJB container will in effect ensure serialization of the requests by applying DB locks for each transaction which is running in a thread. If a developer were to spawn or manage application threads, these may well clash with the threads which the container itself is creating and managing.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yuryi,

Tell me please if this thread is useful to you:

http://www.coderanch.com/t/316794/EJB-JEE/java/CMP-insert

Regards.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted a way to do this but neglected to say it's probably a really bad idea. Use the database to control data concurrency. It's really good at it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic