title: Concurrent Writing & Reading with JPA2/Hibernate
body: <p>I'd appreciate help in understanding why my application fails to find an entity, even though it exists in the database; I believe the issue is related to concurrent writing/reading. I'm using jpa2/hibernate 4 and spring 3.</p>
  
  <p>I have a method that creates a user then sends the id, as a json object message, to a message queue where the user is further processed. Problem occurs when the message handler (UserProcessor.class) attempts to <code>find</code> the user (see below).</p>
  
  <p><strong>Registration.class</strong></p>
  
  <pre><code>@Transactional
  public Response createUser(String firstName, String lastName) {
    User tmpUser = new User(firstName, lastName);
    User savedUser = this.em.merge(tmpUser);
  
    this.em.flush();
  
    if (savedUser != null) {
      processUser(savedUser.getId()); // message sent to queue.
    } else {
      // Throw exception...
    }
  }
  </code></pre>
  
  <p><strong>UserProcessor.class</strong></p>
  
  <pre><code>@Transactional(rollbackFor={javax.ws.rs.WebApplicationException.class})
  public void processUser(Long id) {
    User user = this.em.find(User.class, id); // No user entity is found, "user" is null.
    if (user == null) {
      // throw exception
    }
    ...
  }
  </code></pre>