title: Deadlock in Hibernate query
body: <p>I have scoured the internet but haven't found anything on below. Please let me know if I have missed anything.</p>
  
  <p>We are experiencing c3po's "Apparent Deadlock" exception in our server. PFB the DAO function:</p>
  
  <pre><code> try{
     criteria = session.createCriteria();
     ....... // restriction et al.
     List&lt;MyObject&gt; myObj = (List&lt;MyObj&gt;) criteria.list();
   }catch(Exception e){ //exception handling }
   }finally{ session.close();  }
  </code></pre>
  
  <p>We started seeing the deadlock exception when we added below code in MyObject.class</p>
  
  <pre><code> public class MyObject{
    ...//member variables and methods
    private Set&lt;CustomClass&gt; variable;
  
    public void setVariable(Set&lt;CustomClass&gt; variables){
       variables.stream().sorted((e1, e2) -&gt; e1.myFn()
                  .compareTo(e2.myFn())); //encountered deadlock exception after adding this line
       this.variables = variables;
    }
   }
  </code></pre>
  
  <p>I removed the sorting code and added an order-by clause in hbm mapping to fix the deadlock exception. 
  My question is this - <strong>why would this introduce a deadlock in the first place?</strong></p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit:</strong> Corrected typo in code</p>