-->
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.  [ 8 posts ] 
Author Message
 Post subject: EntityManager problems
PostPosted: Fri Jul 28, 2006 11:22 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I am currently experiencing some problem using hibernate (version that ships with JBoss 4.0.4GA) with postgres 8.1.4 in an EJB3.0 container. 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

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

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

Code:
<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 ??

Thanks,

Andy


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 6:55 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

After spending the whole week trying to fix this problem, i am still no closer to the actual solution but i have discovered some more information. This problem is Postgres specific at the moment as i have tested the EntityManager.find() method call in the same context when using Hypersonic and MySQL and the entity is always found.

After studying the log file created by Postgres (without having the 2 second delay) it seems to not be able to commit the initiial insert statement generated from the EntityManager.persist() call before it tries to query it with the select statement, thus the .find() returns null. The thing that worries me about this problem is that some times it works with Postgres, and that it always works with the 2 other databases i tested with.

I have read up on the EntityManager and discovered that it is not thread safe, could this be causing the problem with Postgres, if so i know i need to use an EntityManagerFactory as this is a thread safe object, but how do i just create on factory that the whole of the application can use (be used in different beans, possible JNDI lookup ??), and even then i do not know if this would solve my problem. Is there anyway of making the EntityManager.persist() method wait until the database has actually commited before it returns ??? I have also tried to introduce flush() calls but they do not solve the problem.

Is this a possible bug in the EntityManager or Postgres JDBC code ???

I also have a very simple test application (.ear file) that just persists some bytes to a database and then tries to find them again in a seperate MDB, and then prints out if it cannot find the persisted object. If anybody wants it to test the problem with this application i will make it available to them.


If anyone can help, or offer advice it is appreciated.

Cheers,

Andy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 4:21 am 
Newbie

Joined: Wed Jun 21, 2006 5:46 am
Posts: 10
Location: Finland
I think your Session Bean and Message Driven Bean have their own database connections and transaction boundaries. The problem is that the save operation is not yet committed, when the read operation in the other transactional context is performed. You can try to increase the JDBC Transaction Isolation level, to get the Message Bean connection to wait for the Session Bean connection to commit it's work.

If you are using JBoss JCA data sources, you can configure the transaction isolation levels in jboss configuration files.

http://docs.jboss.org/jbossas/jboss4gui ... .jdbc.sect

There is also a hibernate propety for connection isolation levels:
http://www.hibernate.org/hib_docs/v3/re ... n-optional

hibernate.connection.isolation

Try the values 4 and 8, and see if it helps. The higher isolation level means that more care is taken to avoid conflicts in Database.

4 = TRANSACTION_REPEATABLE_READ
8 = TRANSACTION_SERIALIZABLE


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
hug, I answered this question somewhere already. Are you crossposting?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 4:03 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
I tried to set the transaction isolation levels to read_repeatable and also to read_serialized and it did not have any effect on the problem.

Yesterday we managed to get it working with pure JDBC without any problems. We then integrated the EntityManager with the JDBC statements, and we think the problem exists with the .persist() method. It seems as if it returns from the method without waiting for the commit to happen at the database level.

Andy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 10:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
persist has nothing to do with the DB commit. persist either trigger an I§NSERT right away or delay it until the next flush operation

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 9:13 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
This was fixed by using the following tag:

Code:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

Thanks,

Andy


Top
 Profile  
 
 Post subject: Re: EntityManager problems
PostPosted: Wed Oct 21, 2009 12:05 pm 
Newbie

Joined: Wed Apr 11, 2007 8:48 am
Posts: 13
Andydale, I am facing the same problem. Where did you add this tag, at SessionBean level or at method level?

If it is to be added in the SessionBean level, will it effect rest of the functionality of the stateless SessionBean. Uptil now I am letting JBoss handle the transactions.

Thanks


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

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.