[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - EntityManager not working correctly

andydale do-not-reply at jboss.com
Sat Jul 29 04:19:57 EDT 2006


I am currently experiencing some problems using the that JBoss 4.0.4GA persistence with postgres 8.1.4. I am having lots of trouble reading Entities back out of the database.

The application works as follows, data is received and a new object (Entity) is created and then persisted from within a stateless session bean. The a message is sent to a Message Driven Bean where the entity is then attempted to be read out of the database (with EntityManager.find()) and this is where the problem is. Most of the time the find() method cannot find the Entity even though it has been persisted (verified persistence by viewing the table in pgAdmin) but now this is where it gets interesting, if you code the EntityManager.find() code as so

<object> = EntityManager.find(<object>.class, Integer.valueof(pk))

null is returned most of the time. But if you code the EntityManager.find() like below:

<object> = EntityManager.find(<object>.class, Integer.valueof(pk))
  | if(<object> == null){
  |            try{
  |               synchronized(this){
  |                  wait(2000);
  |               }              
  |               <object> = EntityManager.find(<object>.class, Integer.valueof(pk))
  |            }
  |            catch(InterruptedException e){
  |               e.printStackTrace();
  |            }
  | }

the object is always found on the second call of the find method. Has anybody else experienced this strange behaviour ????

Another few things to note are that if i call the find method directly after persisting the Entity in the stateless session bean then is is always found, and in both beans the EntityManager is injected with the @PersistenceContext using the same persistence unit . Also note that the stateless session bean and the message driven bean are in different archives in a .ear file, would this make a difference ??



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3961673#3961673

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3961673

More information about the jboss-user mailing list