hibernate关于session的关闭问题
来源:互联网 发布:投影机融合软件 下载 编辑:程序博客网 时间:2024/05/02 02:24
[java] view plaincopy
- Student student = new Student();
- student.setName("Jan");
- student.setAge("22");
- student.setAddress("广东省肇庆市");
- Session session =HibernateUtil.getSessionFactory().getCurrentSession();
- session.beginTransaction();
- session.save(student);
- session.flush();
- session.getTransaction().commit();
1、getCurrentSession()与openSession()的区别?
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession(),创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession(),创建的session必须手动关闭
2、使用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() 有何不同和关联呢?
在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
许多时候出现session is close();原因就是你在hibernate.cfg.xml里面设置了
[html] view plaincopy
- <property name="hibernate.current_session_context_class">thread</property>
系统在commit();执行完之后就关闭了session,这时候你手动再关闭session就当然提示错误了
0 0
- hibernate关于session的关闭问题
- hibernate关于session的关闭问题
- hibernate关于session的关闭问题
- hibernate关于session的关闭问题
- Hibernate中session的关闭问题
- 解决Hibernate的Session的关闭与开启问题
- 项目不定时报hibernate的session没有关闭的问题
- 解决Hibernate的Session的关闭与开启问题
- hibernate一对多session关闭问题
- hibernate延迟加载和session关闭问题
- 关于Hibernate的Session
- 关于关闭浏览器后清除session的问题
- 关于浏览器关闭之后session就变了的问题
- 关于session未能正常关闭造成界面假死的问题
- 关于浏览器关闭后session是否被删除的问题
- Hibernate的session问题
- spring 控制hibernate的session何时关闭.
- spring 控制hibernate的session何时关闭.
- 内存分配2
- RTMP规范简单分析
- 基于MFC和winpcap的简单路由器实现
- Js获取当前日期时间及其它操作
- java打地鼠游戏
- hibernate关于session的关闭问题
- ABAP中使用for all entries in小结
- 平台信息数据库:QPServerlnfoDB
- 学习编程的七个阶段
- iOS 项目中用到的一些 iOS 开源库和第三方组件
- SVN myeclipse
- 重装mysql安装时需要输入current password的解决办法
- 数据库事务
- Ubuntu 14.04 安装HexChat