session详解

获的session对象的方式:

  1.openSession

  2.getCurrentSession

如果使用getCurrentSession需要在hibernate.cfg.xml文件中进行配置:

  如果是本地事务(jdbc事务):<property name="hibernate.current_session_context_class">thread</property>

  如果是全局事务(jta事务):<property name="hibernate.current_session_context_class">jta</property>

openSession与getCurrentSession的区别

  1.getCurrentSession在事务提交或者回滚之后会自动关闭,而openSession需要手动关闭。如果使用openSession而没有手动关闭,多次之后会导致连接池溢出。(getCurrentSession每次获得session是一样的,openSession获得的是不同的)

  

原文地址:https://www.cnblogs.com/jasonlixuetao/p/5928380.html