title: Hibernate and spring with weblogic and creation of SessionFactories through code - solved albeit not so satisfying a solution
body: <p>Question is very similar to what I asked earlier:<br>
  <a href="https://stackoverflow.com/questions/27549538/hibernate-spring-weblogic-and-transaction-management#comment43532319_27549538">https://stackoverflow.com/questions/27549538/hibernate-spring-weblogic-and-transaction-management#comment43532319_27549538</a><br>
  <a href="https://stackoverflow.com/questions/27344393/transaction-management-using-hibernate-with-weblogic#comment43261545_27344393">Transaction Management using Hibernate with weblogic</a><br>
  Using Oracle db , weblogic app server , datasource configured in weblogic server<br>
  Creating SessionFactories in code rather than by using application context and bean definition<br>
  @m-deinum - thanks for your inputs and yes sorry - I know you have been questioning me on why  am doing it this way and that I should be looking at <strong>multi tenant support</strong> - I will - but call me pig-headed - just wanted to get this closed :)<br>
  So I got this to work - albeit - not a clean solution<br>
  Please help me understand why it works and why something that should work is throwing exceptions!<br>
  Hibernate.cfg.xml: ( which contains weblogic datasource )  </p>
  
  <pre><code>&lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
     &lt;property name="connection.datasource"&gt;MYDS&lt;/property&gt;
     &lt;property name="show_sql"&gt;true&lt;/property&gt;
     &lt;property name="default_schema"&gt;CLIENT1SCHEMA&lt;/property&gt;
     &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
   &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>NOTE - have used 'thread' in 'current_session_context_class'  </p>
  
  <p>Code that works:  </p>
  
  <pre><code>public List&lt;T&gt; findAll(final HashMap param) {
    List&lt;T&gt; listData = null;
    Criteria subSelectCriteria =null;
    try {
     Session session=getSessionFactory().getCurrentSession();
     Transaction tx = session.beginTransaction();
     subSelectCriteria =session.createCriteria(getType());
    } catch (HibernateException e) {
      e.printStackTrace();
    }
    boolean isDistinctQuery=false;
    subSelectCriteria.setFirstResult(0);
    subSelectCriteria.setMaxResults(100);
    List inObjList = subSelectCriteria.list();
    if(!inObjList.isEmpty()){
      listData = inObjList;
    } 
    /** one of the below needs to be commented for this to work !!! - which makes no sense ? **/
    tx.commit();
    session.close();
    return listData;
  </code></pre>
  
  <p>}</p>
  
  <p>If I commit and then close the session am getting a weird error :  </p>
  
  <blockquote>
    <p>org.hibernate.SessionException: Session was already closed</p>
  </blockquote>
  
  <p>Now - if I comment out either tx.commit OR session.close code works fine !<br>
  why ?<br>
  is this ok ?<br>
  Thanks
  @m-deinum - will definitely start exploring the Multitenant approach and I actually spent a day - but I did not get an end to end implementation / example I could follow  </p>