title: Race condition on MDB: which is the best way to handle it?
body: <p>I have an event-driven application based on MDB, spring integration and JPA for persistence.
  The application is deployed on weblogic 10.3 with a max-pool-size default value (16).</p>
  
  <p>My application need to handle some messages that modfies DB.
  Imagine that I have use case where I have to handle some messages that for their logic need to insert a domain object if it does not exist or update it if it exists.</p>
  
  <p>So my component that modifes the DB check if the object exists or not and then call the merge on JPA.
  Since two concurrent messages are executed in parallel when I call the merge on dao, JPA triggers two insert statement since the object is not yet persisted and so only one of them is correctly executed.</p>
  
  <p>Is there a pattern to handle this kind of "race condition" in an event-driven application?</p>
  
  <p>Kind regards
  Massimo</p>