Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with removing and transactions

 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamad Norouzi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic