users@glassfish.java.net

Entity Manager Issue

From: <glassfish_at_javadesktop.org>
Date: Mon, 21 Jun 2010 22:51:59 PDT

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.
[Message sent by forum member 'pushpakde']

http://forums.java.net/jive/thread.jspa?messageID=475319