persistence@glassfish.java.net

Re: PreRemove ConcurrentModificationException

From: Wonseok Kim <guruwons_at_gmail.com>
Date: Wed, 20 Jun 2007 22:19:21 +0900

Hi,

a little late to continue this thread :-)
but I'm concerned that without your @PreRemove callback it could cause
commonly known cache inconsistency when removing an ArticlePlacement entity
:(. Article and ArticleGroup entities who had a reference to
ArticlePlacement could still have the reference even after removing
ArticlePlacement. To avoid this you should remove the reference from
referenced entities.

your PreRemove callback could be an way to accomplish this, but that code
would cause ConcurrentModificationException when ArticlePlacement is removed
by cascade remove from Article(em.remove(article)). This is because TopLink
calls cascade remove for ArticlePlacement(and its PreRemove callback) while
it is iterating through Article.articlePlacement list(bug?). As you know
ConcurrentModificationException occurs if there is a modification while
iterating though a list.

Another way is to manually remove references to ArticleGroup before
em.remove() not by PreRemove callback :(.
I think your PreRemove callback would be no problem if there is no cascade
remove and it looks elegant than manual remove.

I'm curious even cascade remove this could be done elegantly with PreRemove
callback. Any idea?

--- your code snippet ---
Article:

 @OneToMany(mappedBy = "article", cascade=CascadeType.ALL)
 private List<ArticlePlacement> articlePlacements = new
ArrayList<ArticlePlacement>();

ArticleGroup:

 @OneToMany(mappedBy = "articleGroup", cascade={CascadeType.PERSIST,
CascadeType.MERGE,CascadeType.REFRESH})
 @OrderBy("position ASC")
 private List<ArticlePlacement> articlePlacements = new
ArrayList<ArticlePlacement>();

ArticlePlacement:

 @ManyToOne
 private Article article;

 @ManyToOne
 private ArticleGroup articleGroup;

 @PreRemove
 public void beforeRemove()
 {
   getArticleGroup().removeArticlePlacement(this);
   getArticle().removeArticlePlacement(this);
 }
---
Thanks,
Wonseok
On 6/15/07, Greg Ederer <greg_at_ergonosis.com> wrote:
>
> Fantastic!  It works perfectly now without the @PreRemove callbacks.
>
> I'll be more cognizant of the mappedBy attribute in the future.
>
> Thanks!
>
> Greg
>
>