• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Atomic operation of two entity invocations from a session bean

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a Stateless Session Bean which does the following:

******Inside the business method*************
boolean entityBeanExists = find an entity bean

if (entityBeanExists == false)
create the entity bean
******************************************

Now, I want above operations to be atomic.
I don't want two simultaneous calls from 2 instances of the Pooled Stateless Bean to execute this code and create two instances of the entity bean.
I only want one entity bean to be created even if the above code is executed simultaneously from two instances of the Stateless Session Bean.
This can be done only if the above code is put into a synchronized block, but since we are not allowed to use the keyword "synchronized" inside EJBs, how can I achieve this effect?

Thanks.
Mathew
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way you can do is :
Lock the table where the entity is going to be created.
Execute your check and create code block in a transaction.
Make sure that the transaction is closed and the changes are committed so that any other transaction is aware of these changes.
Now unlock the table.

This way i hope you can achieve what you are looking for.
I would be interested to hear other better approaches from the experts here.
 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but I hope that somebody can post a simpler solution to this problem . . . .
 
lokesh sree
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
Can someone suggest if there is any simpler solution? Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic