title: Control the hibernate session(when to close it manually)
body: <p>I am new in  hibernate,after read the hibernate api and tutorial,it seems that the session should closed when not used.</p>
  
  <p>Like this:</p>
  
  <pre><code>Session sess=getSession();
  Transcration tx=sess.beginTranscration();
  //do something using teh session
  sess.save(obj);
  tx.commit();
  sess.close;
  </code></pre>
  
  <p>I have no question when using it in a standlone application.
  However I am not sure when using in the web app.</p>
  
  <p>For example, I have a servlet: <code>TestServlet</code> to receive the parameters from the client, then I call a Manager to query something according to the parameters, just like this:</p>
  
  <pre><code>class TestServlet{
    doGet(HttpServletRequset,httpServletResponse){
      String para1=request.getParam...();
      String para2=.....
      new Manager().query(para1,para2);
    }
  }
  
  class Manager{
    public String query(String pa1,String pa2){
      Session=....// get the session
      //do query using para1 and 1
      session.close() //Here, I wonder if I should close it.
    }
  }
  </code></pre>
  
  <p>Should I close the session in the query method?</p>
  
  <p>Since someone told me that session in hibernate is just like the connection in jdbc. So opening and closing it so frequently is the correct way?</p>
  
  <p>BTW, does the tx.commit() is required each time?</p>
  
  <p>Also what's the thread problem about using session in servlet, since I saw the session is not thread safe in api.</p>