title: How hibernate ensures second level cache is updated with latest data in database
body: <p>I have read that using hibernate's second level cache, it can improve applications performance by having less database hits for data / object retrieval.</p>
  
  <p>However, how does hibernate ensure that second level cache is up to date with the data in database.</p>
  
  <p>For, example:</p>
  
  <p>Suppose the below class is entity and persisted into the DB.</p>
  
  <pre><code>@Entity
  class User {
      Id
      private int id;
      private String str;
  }
  </code></pre>
  
  <p>Now, if we have enabled second level cache, I understand that if we open different sessions then each session will hit the second level cache for retrieving object value.</p>
  
  <p>Now, if data in database gets changes (for e.g. for row with id=1) say by some independent process/ manually changing the values, and we try to access the value, how does hibernate detect that the cache is having latest value (for id = 1).</p>
  
  <p>In general, how does hibernate ensure that data in second level cache is consistent with the db values.</p>
  
  <p>Thanks for your help.</p>