`
jin8000608172
  • 浏览: 138525 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

openSession和getCurrentSession的区别

阅读更多
openSession:获取一个新的Session
getCurrentSession:获得和当前线程绑定的session,在同一个线程中获得同一个session,这样有利于事物的控制。
openSession获得的session也可以维持事物,但必须在同一个service中,getCurrentSession获得的session可以在同一线程的不同service中维持事物。
选择openSession和getCurrentSession原则:
如果需要在同一线程中,保证使用同一个session,选择getCurrentSession
如果在同一线程中,需要不同的session,选择openSession
getCurrentSession获得的session,在commit或者rollback是会自动关闭。
openSession获得的session,需要手动关闭。
建议不管是自动关闭,还是手动关闭都手动关闭
getCurrentSession获得的session需要事物提交,否则会抛异常
getCurrentSession需要配置
hibernate_current_session_context_class thead本地事物
hibernate_current_session_context_class jta全局事物
分享到:
评论

相关推荐

    getCurrentSession 与 openSession() 的区别

    在Java的Hibernate框架中,`getCurrentSession()` 和 `openSession()` 都是用于获取与数据库交互的Session对象,但它们之间存在显著的区别。理解这些差异对于优化数据访问性能和管理事务至关重要。 首先,`...

    SessionFactory.getCurrentSession与openSession的区别

    博文链接:https://shaqiang32.iteye.com/blog/201918

    ssh中getCurrentSession的使用

    2. **为什么使用getCurrentSession()**:与直接调用`openSession()`创建新的Session相比,`getCurrentSession()`有以下优势: - 它能够自动管理Session的生命周期,比如在请求结束时关闭Session,避免资源泄露。 -...

    新Hibernate SessionFactory().getCurrentSession()猫腻

    当我们调用SessionFactory().getCurrentSession()时,Hibernate会为我们提供一个已存在的或者新创建的Session实例,这个行为与直接调用SessionFactory.openSession()有所不同。`getCurrentSession()`方法旨在支持...

    Spring视频教程(3)

    2. **编程式事务与OpenSession/GetCurrentSession的区别**: 编程式事务处理是手动管理事务边界,而OpenSession/GetCurrentSession模式则是在操作数据库时自动开启和关闭Session。这两者在事务管理和性能上有不同的...

    SSH框架部分常见面试题.pdf

    openSession 和 getCurrentSession 是 Hibernate 中的两个会话方法,openSession 用于创建新的会话,getCurrentSession 用于获取当前会话。 saveOrUpdate() 和 merge() saveOrUpdate() 和 merge() 是 Hibernate 中...

    J2EE考试题目及答案定义.pdf

    openSession()和getCurrentSession()方法都是用于获取Session对象的方法,但它们有所不同。getCurrentSession创建的Session对象会和当前线程绑定,而openSession不会。getCurrentSession创建的Session对象在事务...

    hibernate关于session的关闭实例解析

    本文详细介绍了 Hibernate 中 Session 的关闭实例解析,包括 getSession() 和 openSession() 的区别、getCurrentSession() 的配置、openSession() 和 getCurrentSession() 的关联,以及 Session 的关闭。希望本文...

    SSH-Jsp期末考试简答题带答案

    2. Hibernate 中 openSession()和 getCurrentSession()的差别 Hibernate 是一个基于Java的持久层框架,它提供了对数据库的访问和操作能力。openSession()和getCurrentSession()是Hibernate中两个重要的Session创建...

    hibernate 学习笔记

    hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate

    JAVAEE试题(含答案).pdf

    6. Hibernate 框架中的 SessionFactory 类有两个获得 session 的方法:openSession() 和 getCurrentSession()。如果需要在同一线程中,保证使用同一个 Session 则使用 getCurrentSession(),如果在一个线程中,需要...

    JAVAEE试题(含答案).docx

    6. Hibernate框架中的SessionFactory类:SessionFactory类提供了两个获得session的方法,分别是openSession()和getCurrentSession()。这两个方法的区别在于,openSession()方法可以在一个线程中使用不同的Session,...

    J2EE考试题目及答案.doc

    6. **SessionFactory中的opensession()和GetCurrentSession()**: `getCurrentSession()`创建的Session与当前线程绑定,事务结束后自动关闭,而`openSession()`则不会自动管理,需要手动关闭。`getCurrentSession()...

    Hibernate存储Clob字段的方式总结

    同时,还探讨了Hibernate框架中getCurrentSession()与openSession()的区别。 首先,要操作数据库中的Clob字段,需要关注的是如何在Java对象与Clob字段之间进行转换。由于Clob字段通常用于存储大量文本数据,如果...

    hibernat框架学习

    - **OpenSession vs GetCurrentSession**: - **OpenSession**:每次调用都会创建新的Session。 - **GetCurrentSession**:根据当前线程返回已存在的Session,如果没有则新建一个。 #### 总结 Hibernate作为一种...

    [尚硅谷]_佟刚_Hibernate面试题分析

    8. getCurrentSession()和openSession()的区别: - getCurrentSession()会检查当前线程是否已有Session,若有则返回,若无则创建。 - openSession()方法总是创建一个新的Session。 - getCurrentSession()在事务...

    hibernate笔记心得

    在Hibernate中,我们有两种方式来获取和管理Session:`openSession()`和`getCurrentSession()`。`openSession()`每次都会创建一个新的Session,并且在使用完毕后需要手动关闭,这适用于短生命周期的事务。而`...

    java 面试技术题汇总

    - `getCurrentSession()`和`openSession()`的区别:`getCurrentSession()`会绑定到当前线程,便于事务管理,而`openSession()`需要手动管理Session的生命周期。使用`getCurrentSession()`需要在配置文件中指定适当的...

Global site tag (gtag.js) - Google Analytics