-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate Entity Manager
PostPosted: Mon Jun 21, 2010 8:57 am 
Newbie

Joined: Tue Jul 21, 2009 11:10 am
Posts: 4
Hi All,

I have facing some issue with Entity Manager. I have an web service which internally calls in EJB. There are some predefined methods into EJB. The method to which i am facing the issue is as follows:

Quote:
@stateless
public class SampleEJB{
//inject the Entity manager
EntityManager em;

public method1(){
while(true){
Entity entity = em.createQuery("select Entity where col1 = 'xyz'")

Thread.sleep(5000);

int count = em.createQuery("update Entity set col1 = 'abc'" where col1 = 'xyz').executeUpdate();

if(count > 0 )
break;

}
}
}


Suppose that there is only one record into the database with col1 = xyz. Issue is visible when two simultaneous web service request calls the same method1 function.
Following is the scenario:
1. First request calls the web service which internally calls the EJB (new ejb instance).
2. calls the method1 and fetches tha Entity from the database.
3. Goes into sleep.
4. Second web service request comes in calls the same EJB method (new ejb instance).
5. As the col1 is not updated it is able to find the record.
6. second request goes into sleep mode.
7. first request comes out and executes the update command. The count for request one is 1.
8. After which request one breaks the loop and Exits
9 Second request comes out of sleep and tries up the row. but as it is already updated by request one the update count is 0.
10.Request 2 again goes to while loop and again queries for the Entity. And it finds the Entity. Rather the col1 value is changed by request 1.
11. This now happens always. But as we have only one record into the database it should fetch the data.

We are using JTA data source. I feel that Entity Manager is caching the data into Persistence Context.
I am not sure how to handle this issue. I tried EntityManager.clear() before the select query but it didnt worked.

Any help would be Highly appreciated.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.