aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes problem with removing and transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "problem with removing and transactions" Watch "problem with removing and transactions" New topic
Author

problem with removing and transactions

Mohammad Norouzi
Ranch Hand

Joined: Jul 12, 2005
Posts: 71
Hello
I am using Tomlink JPA as persistence provider for EJB3.

The problem has raised when I tried to remove some entities from a collection. As I have some exprience in Hibernate, I thought all the details entities will be removed when I remove them from the collection but soon I realized that in TopLink this is not the case.

now I am removing the entities individually, but the problem is that it seems the entity manager has removed the entities I declared but in database all the records are exists!!!

More stranger, in next execution the deleted entities are not loaded and everything seems ok but if I restart the application server all the removed data is coming back!!

after removing the entity I called entityManager.flush() and even I put TransactionAttribute for the value of REQUIRES_NEW but nothing has changed.

after toiling, I found that if I remove the REQUIRES_NEW from the delete() method of my DAO that works fine.
Would anyone explain me what happens here? As I have read, if you put REQUIRES_NEW the new transaction will start for that method and after exiting the method the transaction will be committed so my assumption is the data is visible to other transaction when it is going to read the same data (including removed records) but this is quite different from my understanding?

would anyone explain what happens here?

thanks


Regards, Mohammad
my.blog | my.photoblog | Add your name to SCJP Wall Of Fame
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
The EntityManager caches object queries. I think you have to call em.refresh on the parent object to signify that a child of his was deleted in the database for finder methods or set a query hint for query methods.

For finder methods :

Lets say we have Object A (parent) which holds a collection of Object B.
If you remove one instance of Object B then query Object A, the EntityManager will give you the same number of Object B, unless you query Object A and force a synchronization by calling em.refresh on Object A for finder methods.

For query hints :

javax.persistence.Query query = em.createNamedQuery("ObjectA.findById");
query.setHint("toplink.refresh", "true");

I hope this helps.

Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
Mohamad Norouzi
Greenhorn

Joined: Oct 12, 2004
Posts: 18
Thanks for your reply, but my problem is not EntityManager's cache. since I use remove() method of the EntityManager there is no query to cache.

My question is, when I put the @TransactionAttribute(REQUIRES_NEW) on the removal method it doesn't remove my entities and when being replaced with REQUIRED, it works fine. why this happens?

thanks again.


<a href="http://pixelshot.wordpress.com" target="_blank" rel="nofollow">http://pixelshot.wordpress.com</a><br /><a href="http://brainable.blogspot.com" target="_blank" rel="nofollow">http://brainable.blogspot.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with removing and transactions