`

openSession & getCurrentSession

    博客分类:
  • java
 
阅读更多
1. openSession 从字面上可以看得出来,是打开一个新的session对象,而且每次使用都是打开一个新的session,假如连续使用多次,则获得的session不是同一个对象,并且使用完需要调用close方法关闭session。
  2. getCurrentSession ,从字面上可以看得出来,是获取当前上下文一个session对象,当第一次使用此方法时,会自动产生一个session对象,并且连续使用多次时,得到的session都是同一个对象,这就是与openSession的区别之一,简单而言,getCurrentSession 就是:如果有已经使用的,用旧的,如果没有,建新的。

注意 :在实际开发中,往往使用getCurrentSession多,因为一般是处理同一个事务(即是使用一个数据库的情况),所以在一般情况下比较少使用openSession或者说openSession是比较老旧的一套接口了;

对于getCurrentSession 来说,有以下一些特点:
1.用途,界定事务边界
2.事务提交会自动close,不需要像openSession一样自己调用close方法关闭session
分享到:
评论

相关推荐

    getCurrentSession 与 openSession() 的区别

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

    SessionFactory.getCurrentSession与openSession的区别

    理解 `SessionFactory.getCurrentSession()` 和 `Session.openSession()` 的区别对于优化和正确使用Hibernate至关重要。 首先,`SessionFactory` 是Hibernate中的一个接口,它是一个全局的、线程安全的工厂类,用于...

    ssh中getCurrentSession的使用

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

    新Hibernate SessionFactory().getCurrentSession()猫腻

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

    Spring视频教程(3)

    编程式事务处理是手动管理事务边界,而OpenSession/GetCurrentSession模式则是在操作数据库时自动开启和关闭Session。这两者在事务管理和性能上有不同的考量。学习者将了解到何时选择哪种方式,以及它们在Spring中...

    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 的关闭。希望本文...

    hibernat框架学习

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

    Hibernate存储Clob字段的方式总结

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

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

    openSession()和getCurrentSession()是Hibernate中两个重要的Session创建方法。采用getCurrentSession()创建的Session会绑定到当前的线程中去,而采用openSession()则不会。getCurrentSession()创建的Session在...

    hibernate 学习笔记

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

    J2EE考试题目及答案.doc

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

    JAVAEE试题(含答案).pdf

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

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

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

    hibernate笔记心得

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

    JAVAEE试题(含答案).docx

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

    java 面试技术题汇总

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

Global site tag (gtag.js) - Google Analytics