This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Atomic operation of two entity invocations from a session bean  RSS feed

 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • 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
 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
Can someone suggest if there is any simpler solution? Thanks.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!