title: How to synchronize concurring Web Service calls in Java
body: <p>I'm currently developing some web services in Java (&amp; JPA with MySQL connection) that are being triggered by an SAP System.
  To simplify my problem I'm referring the two crucial entities as <code>BlogEntry</code> and <code>Comment</code>. A <code>BlogEntry</code> can have multiple <code>Comments</code>. A <code>Comment</code> always belongs to exactly one <code>BlogEntry</code>.</p>
  
  <p>So I have three Services (which I can't and don't want to redefine, since they're defined by the WSDL I exported from SAP and used parallel to communicate with other Systems): <code>CreateBlogEntry</code>, <code>CreateComment</code>, <code>CreateCommentForUpcomingBlogEntry</code></p>
  
  <p>They are being properly triggered and there's absolutely no problem with <code>CreateBlogEntry</code> or <code>CreateComment</code> when they're called seperately.</p>
  
  <p><strong>But:</strong> The service <code>CreateCommentForUpcomingBlogEntry</code> sends the <code>Comment</code> and a "foreign key" to identify the "upcoming" <code>BlogEntry</code>. Internally it also calls <code>CreateBlogEntry</code> to create the actual <code>BlogEntry</code>. <strong>These two services are - due to their asynchronous nature - concurring.</strong></p>
  
  <p>So I have two options:</p>
  
  <ol>
  <li>create a dummy <code>BlogEntry</code> and connect the <code>Comment</code> to it &amp; update the <code>BlogEntry</code>, once <code>CreateBlogEntry</code> "arrives"</li>
  <li>wait for <code>CreateBlogEntry</code> and connect the <code>Comment</code> afterwards to the new <code>BlogEntry</code></li>
  </ol>
  
  <p>Currently I'm trying the <strong>former</strong> but once both services are fully executed, I end up with <strong>two</strong> <code>BlogEntries</code>. One of them only has the <code>ID</code> delivered by <code>CreateCommentForUpcomingBlogEntry</code> but it is properly connected to the <code>Comment</code> (more the other way round). The other <code>BlogEntry</code> has all the other information (such as <code>postDate</code> or <code>body</code>), but the <code>Comment</code> isn't connected to it.</p>
  
  <hr>
  
  <p>Here's the code snippet of the service implementation <code>CreateCommentForUpcomingBlogEntry</code>:</p>
  
  <pre><code>@EJB
  private BlogEntryFacade blogEntryFacade;
  @EJB
  private CommentFacade commentFacade;
  ...
  List&lt;BlogEntry&gt; blogEntries = blogEntryFacade.findById(request.getComment().getBlogEntryId().getValue());
  BlogEntry persistBlogEntry;
  if (blogEntries.isEmpty()) {
      persistBlogEntry = new BlogEntry();
      persistBlogEntry.setId(request.getComment().getBlogEntryId().getValue());
      blogEntryFacade.create(persistBlogEntry);
  } else {
      persistBlogEntry = blogEntries.get(0);
  }
  
  Comment persistComment = new Comment();
  persistComment.setId(request.getComment().getID().getValue());
  persistComment.setBody(request.getComment().getBody().getValue());
  /*
      set other properties
  */
  persistComment.setBlogEntry(persistBlogEntry);
  commentFacade.create(persistComment);
  ...
  </code></pre>
  
  <hr>
  
  <p>And here's the code snippet of the implementation <code>CreateBlogEntry</code>:</p>
  
  <pre><code>@EJB
  private BlogEntryFacade blogEntryFacade;
  ...
  List&lt;BlogEntry&gt; blogEntries = blogEntryFacade.findById(request.getBlogEntry().getId().getValue());
  BlogEntry persistBlogEntry;
  Boolean update = false;
  if (blogEntries.isEmpty()) {
      persistBlogEntry = new BlogEntry();
  } else {
      persistBlogEntry = blogEntries.get(0);
      update = true;
  }
  persistBlogEntry.setId(request.getBlogEntry().getId().getValue());
  persistBlogEntry.setBody(request.getBlogEntry().getBody().getValue());
  /*
      set other properties
  */
  if (update) {
      blogEntryFacade.edit(persistBlogEntry);
  } else {
      blogEntryFacade.create(persistBlogEntry);
  }
  ...
  </code></pre>
  
  <p>This is some fiddling that fails to make things happen as supposed.</p>
  
  <p>Sadly I haven't found a method to synchronize these simultaneous service calls. I could let the <code>CreateCommentForUpcomingBlogEntry</code> <em>sleep</em> for a few seconds but I don't think that's the proper way to do it. </p>
  
  <p>Can I force each instance of my <code>facades</code> and their respective <code>EntityManagers</code> to reload their datasets? Can I put my requests in some sort of queue that is being emptied based on certain conditions?</p>
  
  <p><em><strong>So: What's the best pracice to make it wait for the <code>BlogEntry</code> to exist?</em></strong></p>
  
  <p>Thanks in advance,
  David</p>
  
  <p>Info:</p>
  
  <ul>
  <li>GlassFish Server 3.1.2</li>
  <li>EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461</li>
  </ul>