title: Safe strategy for saving new entries in a hibernate web app?
body: <p>I have a session-per-request type of a web application. On every request it saves information about user accessing resources. The table for this information has a composite id consisting of the user id and the resource id. I want to keep information only from the last access.</p>
  
  <p>In rare occations (easy to reproduce with code changes) I get <code>SQLIntegrityConstraintViolationException</code>: Duplicate entry errors when two different transactions are trying to insert with the same composite id. My code tries to check if the entry is already found in the database and then inserts it if it's not and otherwise updates.</p>
  
  <p>My question is the following: <strong>How do I safely save new entries?</strong></p>