①openSession,每次创建的都是新的对象,需要手动进行关闭(session.close() ),如果多次打开不关闭,会导致连接池的溢出。
②getCurrentSession,每次创建的都是同一个对象,在事务提交或者回滚之后会自动关闭。
使用getCurrentSession,需要在hibernate.cfg.xml文件中进行配置
<property name="hibernate.current_session_context_class">?</property>
如果为本地事务(jdbc),?为thread
如果为全局事务(jta), ?为jta