-->
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.  [ 2 posts ] 
Author Message
 Post subject: collection concurrency problem
PostPosted: Tue May 27, 2008 10:29 am 
Newbie

Joined: Sun Jul 23, 2006 8:48 am
Posts: 6
I have a unidirectional one-to-many association between an entity 'Media' to an entity 'Comment'.

My problem is the classic concurrency problem. When two users adds a comment each to a media, User A's comment gets overwritten by User B.
This is the store comment logic. It like a transaction isolation level problem but on a table level instead of one particular table row.

Environment:
Hibernate 3.2.6
Jboss 4.2.2.GA
Jboss Seam 2.0.2.GA

Code:
@Name("detailedMediaForm")
@Scope(ScopeType.PAGE)
public class DetailedMediaForm implements Serializable {

   private java.lang.String comment;

   private Media media;

   private Long mediaId;
   
   private Collection<MediaComment> mediaComments;

   // injecting daos

   @Create
   public void initMedia() {
      Media loadedMedia = mediaDAO.findById(mediaId);
   }

   public String postComment() {
      media = mediaDAO.update(media); // does a merge
      refreshComments();
      MediaComment mediaComment = new MediaComment(comment);
      media.getMediaComments().add(mediaComment);
      messageDAO.create(mediaComment);
      mediaDAO.update(media);
      refreshComments();
      return "success";
   }

   public Media getMedia() {
      return media;
   }

   public void refreshComments(){
      mediaComments = mediaDAO.findMediaCommentsByMediaId(mediaId);
   }

   // plumbing methods
   // getters and setters
}


This is the annotation for the comments colleciton in media entity
Code:
@javax.persistence.OneToMany(cascade = {javax.persistence.CascadeType.ALL}, fetch = javax.persistence.FetchType.EAGER)
    @javax.persistence.JoinTable
    (
        name = "MEDIA2MEDIA_COMMENTS",
        joinColumns = {@javax.persistence.JoinColumn(name = "MEDIA_ID_FK", referencedColumnName = "ID")},
        inverseJoinColumns = {@javax.persistence.JoinColumn(name = "MEDIA_COMMENTS_ID_FK", referencedColumnName = "ID")}
    )


The message is persisted in the MESSAGE table but the rows in the jointable MEDIA2MEDIA_COMMENTS between media and comment gets deleted when the link is persisted.
Why does Hibernate delete the links before inserting the new link to my persisted comment?




hibernate output

Code:
15:14:55,078 INFO  [STDOUT] Hibernate: /* insert storage.MediaComment */ insert into MESSAGE (ALLOW_COMMENTS, BODY, CONTENT_PROVIDER_ACCOUNT_FK, DATE, TITLE, TYPE) values (?, ?, ?, ?, ?, 'M')
15:14:55,218 INFO  [STDOUT] Hibernate: /* delete collection convert.frontend.storage.Media.mediaComments */ delete from MEDIA2MEDIA_COMMENTS where MEDIA_ID_FK=?
15:14:55,218 INFO  [STDOUT] Hibernate: /* insert collection row convert.frontend.storage.Media.mediaComments */ insert into MEDIA2MEDIA_COMMENTS (MEDIA_ID_FK, MEDIA_COMMENTS_ID_FK) values (?, ?)
15:14:55,218 INFO  [STDOUT] Hibernate: /* insert collection row convert.frontend.storage.Media.mediaComments */ insert into MEDIA2MEDIA_COMMENTS (MEDIA_ID_FK, MEDIA_COMMENTS_ID_FK) values (?, ?)


thanks.


Top
 Profile  
 
 Post subject: Re: collection concurrency problem
PostPosted: Thu May 29, 2008 4:47 am 
Newbie

Joined: Sun Jul 23, 2006 8:48 am
Posts: 6
I found a temporary solution that works.

I moved the "postComment logic" into another bean (EVENT-scoped Seam component). I load a fresh media object from db and store the comment on to that media objects collection of comments.

Do anyone have an idea of how I could hold the fetched comments in the same bean as where I store the new comment and also refresh the comments collection before I store the new one?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.