title: spring singleton object state
body: <p>Here I have one doubt with <strong>spring singleton object</strong>. </p>
  
  <blockquote>
    <p>Spring will create atleast one object per bean definition depending on the scope.
    for singleton scope, you will get only one object per bean definiton.</p>
  </blockquote>
  
  <p>As spring context provide us singleton instance then why I am not able to share same session of it. </p>
  
  <p>The below code give error:</p>
  
  <blockquote>
    <p>Throw null pointer error.
    org.hibernate.TransactionException: Transaction not successfully started</p>
  </blockquote>
  
  <p><strong>while trying to access same instance session. even session instance is null.</strong></p>
  
  <p>In  main method</p>
  
  <pre><code>public static void main(String[] args)
  {
    TestDao dao =  (TestDao) ApplicationContext.getInstance().getBean(DaoType.TestDao.toString());
    dao.startOperation();
    for(Test test:testList)
    {
       saveIsBean(test,true)
    }
    dao.endOperation();
  }
  </code></pre>
  
  <p>This method save data if session open then reuse it.    </p>
  
  <pre><code>private void saveIsBean(IsBean isBean,boolean isSessionAlreadyOpen) throws NTException
   {
   TestDao dao =  (TestDao) ApplicationContext.getInstance().getBean(DaoType.TestDao.toString());
    if(isSessionAlreadyOpen)
   {
   //dao.startOperation(); If I start session again then it works.
   dao.getSession().saveOrUpdate(isBean); //Throw null pointer error. org.hibernate.TransactionException: Transaction not successfully started
    }
   else
   {
   dao.saveOrUpdate(isBean);
    }
   } 
  public void startOperation() throws HibernateException {
   session = HibernateFactory.openSession();
   transaction = session.beginTransaction();
   }
  </code></pre>
  
  <p>If I start session again  or pass same session instance in method then it works fine but I do not understand why it does not work. <strong>singleton instance does not have same state in spring context!!</strong></p>
  
  <p>Thanks In Advance</p>