一般来说不需要自己控制session,spring会帮你管理。
如果需要使用session进行flush(),evict操作可以直接获得getSession()
如果想使用hibernate原生的api,需要使用HibernateCallback,它会帮助你控制事务。

解决方案 »

  1.   

    一般项目都会用到连接池.边接池会帮你管理.不用CLOSE
      

  2.   

    他问的是session,不是Connection。
    session是hibernate里的一级缓存会话。
      

  3.   

    Spring能不能为我管理由hibernateTemplate.getSessionFactory().getCurrentSession()得到的session?
      

  4.   

    spring的配置文件中你所配置的sessionFactory应该有提供destroy-method那个属性可以设置为close
      

  5.   

    在设计视图下右键点sessionFactory那个BEAN添加一个叫destroy-method的Attribute就看见了是设置sessionFactory的销毁方法数据源也有这个属性
      

  6.   

    有spring的话,session可以不用我们去管理。spring可以很灵活的管理session和事务,在适当的时机提交事务和关闭session。
      

  7.   


    我觉得getSession()跟你这个获得的效果应该是一样的。
    destroy-method
    这个不是管理session的,这个是为了让spring知道,在销毁applicationContext的时候,执行哪个特定方法释放bean中占用的资源的。
    小心别用混了。
      

  8.   

    session由sessionFactory自己管理,不要看见了connection 用完要close之后就画蛇添足,session是轻量级的,不占多少资源的,当我们在执行完某些操作的时候他自己就清理缓存了,比如save update等操作,session没有close方法,类似的是evict,一般不提倡自己evict。
      

  9.   

    session是有close()方法的。另外:
    hibernateTemplate.getSessionFactory().openSession()是spring调用hibernate的API打开了一个session。spring会去管理这个session吗?hibernateTemplate.getSessionFactory().getCurrentSession()貌似是取得了当前的session,DOC是这么说的:
     “Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl     configured for use.”
    这个session是不是一直被spring管理着呢?
      

  10.   

    hibernateTemplate的一个最初目的就是简化hibernate。
    session关闭这种事当然不用考虑了。
      

  11.   

    通过hibernateTemplate取得的session 是被Spring管理的
    不需要自己关闭
      

  12.   

    那session中的缓存呢?不是还要session自己管理么?
      

  13.   

    不用了,hibernate 已经交给spring管理了,在spring的配置中destory-method=close就可以了