aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Is there a way to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Is there a way to "synchronize" a stateless session bean Watch "Is there a way to "synchronize" a stateless session bean New topic
Author

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

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Why would you wish to prevent the same method from being invoked by concurrent clients?


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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

Joined: Feb 17, 2005
Posts: 704
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.


I think, therefore I exist -- Rene Descartes
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Feb 17, 2005
Posts: 704
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

Joined: Jan 29, 2003
Posts: 8791
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is there a way to "synchronize" a stateless session bean's business method?