Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing an EJB method  RSS feed

 
Rob Micah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a stateless bean that has a method which calls a stored procedure on an Oracle database. I need to make sure this method does not run concurrently. How can I do this? Also I should mention I'm stuck using J2EE 1.4.
 
Rob Micah
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading through the EJB specification I can see this is impossible. So now I have another question. If I used a synchronize block inside my servlet on a static method could that accomplish the same thing?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are looking for is a @javax.ejb.Singleton which exposes a method with (default) Lock.WRITE semantics. This has been introduced in Java EE 6 which most servers support these days. But since you say, you are stuck with J2EE 1.4, you won't be able to use it.

So now I have another question. If I used a synchronize block inside my servlet on a static method could that accomplish the same thing?

I don't think that's a good idea. This approach will end up blocking thread(s) that serve the web application. This not just means blocking the invocation on that specific servlet but also can lead to threads not being available to serve other parts of the web application. Users will end up seeing a "hung" browser.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't fully know your usecase, but I think you could look for making this asynchronous and handling one request at a time. You could have a single instance of a message driven bean (so that it processes only one message at a time) which would trigger the stored procedure execution. You would then have to just push messages to a queue which would then be consumed by the MDB. It however is a application server specific detail on how to configure a message driven bean to just use a single instance. So you'll have to check the documentation of whichever server you are using.

 
Joemon Anthony
Greenhorn
Posts: 26
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Micah wrote:If I used a synchronize block inside my servlet on a static method could that accomplish the same thing?


Never do this. By doing this you will end up by locking the whole class. So other requests will have to wait, for this request(which has acquired lock) to complete. Eventually will become a big bottleneck. So avoid this.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!