• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using synchronized block in stateless session bean

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to use synchronized block in stateless session bean.

I need to use it because the block should be used only by one user at a time. I know its a performance degradation, but will it work properly.

Since I used it for testing and it seems to be working, but I am not sure how and why? I tried searching for an explanation, but was not successful.

Any help appreciated.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I need to use it because the block should be used only by one user at a time


A synchronized block is not going to guarentee that. What happens when the ejb is deployed in multiple containers, and hence multiple JVMs?
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The system will not be deployed in multiple JVM's for foreseeable future. But I understand what you are trying to imply.

So is there any other way to implement that.

Thanks
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would need to implement your own locking on something else, like a database record.

Messy, non-standard, and prone to failure.
Far better to redesign your application to be able to deal with multiple concurrent users.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be wrong but a stateless session bean will be served to one client at time by the container and it will put back to the pool for later reuse. In this context why does your bean need take of synchronization of some part of the code.?? in the first place. Container will take care that one bean will serve one client and that no two clients are accessing the same stateless bean instance.
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies. As of now, its not feasible to redesign the system.

Actually I want the restrict the insertion of duplicate records, when the user clicks the save button multiple buttons.

So before insertion, I am checking if those records are there previously, and then insert if there are no records.

But what happened was, when the user clicked the save button multiple times, the first control will be inserting and would not have committed, the second control goes and checks if any records are present, since the first transaction was not committed, there are no records, so the 2nd time also the insertion takes place.

To aviod this, I was trying out different scenarios.

But now I have dropped the synchronization idea, but any other suggestions would be appreciated.

Thanks
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you think that your logic will be an overhead for the server to check the record in the database before inserting a new one.
Instead my take on this would be to catch an exception and displaying it to the user typecasted with business exception.

Thanks,
Rahul
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better write a javasript function on save button click, so that at second click on same button , it will display an error message like "Request is in process.........". This will prevent the same request to submit multiple times & then there will not be any SQlException.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a java script to disable the save button as soon as the user click the button by this way you are safe and you dont have to think about the super duper logic..

cheers
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronization is one of the services offered by the EJB container. That is, among others, the beauties that it can offer us.

Instead of worry about that, focus on the business requirement.
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies. I can throw an exception, but the problem is the records for that table will be inserted from many places, as well as inserted manually. So at any time there should be only one record, and that one record will be used for further processes.

I have now used javascript to disable the save button.

Synchronization is one of the services offered by the EJB container. That is, among others, the beauties that it can offer us.



Gabriel, can you please elobrate on this.

Thanks again.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic