`

hibernate的session管理

阅读更多

SessionFactory.get CurrentSession openSession 区别
    1. 如果使用的是get CurrentSession 来创建session的话,在commit后,session就自动被关闭了,
         也就是不用再session.close()了。但是如果使用的是openSession 方法创建的session的话,
         那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭
2. get CurrentSession 的使用可以参见hibernate\hibernate-3.2\doc\tutorial\src项目
3. 使用SessionFactory.get CurrentSession ()需要在hibernate.cfg.xml中如下配置:
   * 如果采用jdbc独立引用程序配置如下:
    <property name="hibernate.current_session_context_class">thread</property>
   * 如果采用了JTA事务配置如下
    <property name="hibernate.current_session_context_class">jta</property>

 

4。Do I still need to configure Hibernate to bind Sessions (hibernate.current_session_context_class=thread) to the thread stack? Does Spring take care of this automatically?

If you use LocalSessionFactoryBean, you don't need to declare hibernate.current_session_context_class=thread, Spring will manage it for you . You only declare that if you're going to encapsulate access to the session factory manually (e.g. HibernateUtil). This is also related to the use of SessionFactory.getCurrentSession().

There is a big difference on how the SessionFactory.getCurrentSession() works on plain hibernate and Spring. If you use Hibernate directly, getCurrentSession() works by returning the current session being used, or it spawns a new session. For the case of Spring (via LocalSessionFactoryBean), the getHibernateTemplate().getCurrentSession() does return the current session being used, but it does not spawn a new one if none is available.

 

5. LocalSessionFactoryBean Set connection release mode "on_close" as default.
 This was the case for Hibernate 3.0; Hibernate 3.1 changed
 it to "auto" (i.e. "after_statement" or "after_transaction").
  However, for Spring's resource management (in particular for
HibernateTransactionManager), "on_close" is the better default.

分享到:
评论

相关推荐

    Hibernate Session释放模式

    这是最常见的Session管理方式,通常与Spring的@Transactional注解结合使用。每个数据库事务开始时创建Session,事务结束时关闭Session。这种方式简单且易于理解,能保证事务的ACID特性,但可能会导致大量的短连接,...

    Hibernate(session管理)

    本文将深入探讨Hibernate中的核心概念——Session管理。 首先,理解Session在Hibernate中的角色至关重要。Session是Hibernate的主要工作单元,它是应用程序与数据库之间的桥梁。它负责保存、检索和更新Java对象,...

    Hibernate-nosession

    在这种情况下,使用Hibernate-nosession模式可以避免创建和管理Session的开销,从而提高应用性能。nosession模式并不意味着完全不使用Hibernate,而是尽量减少对Session的依赖,转而使用其他API,例如Query或...

    hibernate session生命周期示例

    通过合理的Session管理,我们可以减少数据库交互的复杂性,提高程序性能,并避免数据一致性问题。实践中,还要注意避免频繁打开和关闭Session,以及合理控制事务的粒度,以达到最佳的性能效果。

    Hibernate_Session_Transaction

    在Java的持久化框架Hibernate中,`Session`和`Transaction`是两个至关重要的概念,它们在数据管理和事务处理中起到核心作用。这篇文章将深入解析这两个概念及其相关知识点。 `Session`是Hibernate提供的一种与...

    hibernate中session的管理

    在实际开发中,除了使用ThreadLocal,还有其他几种Session管理策略,例如: 1. **Session per Request/Transaction**:每个HTTP请求或数据库事务分配一个Session。这通常是Web应用程序的最佳实践,因为HTTP请求是...

    hibernate和session学习

    【hibernate与Session详解】 Hibernate 是一个强大的Java对象关系映射(ORM)框架,它为开发者提供了在Java应用程序中操作数据库的强大工具。通过Hibernate,我们可以将数据库中的表与Java类进行映射,从而实现对...

    对Hibernate Session做一个简单的包装

    本篇文章将探讨如何对Hibernate的`Session`进行简单的包装,以提升代码的可维护性和灵活性。`Session`是Hibernate的核心组件,它负责对象的持久化操作,如保存、更新、查询等。 `Session`接口提供了多种方法来与...

    重写hibernate的session简单增删改查

    Session提供了事务管理和缓存管理的功能,确保了数据的一致性和性能。 1. 增加(Create) 在Hibernate中,增加数据通常通过调用Session的save()或saveOrUpdate()方法来实现。save()用于新对象的保存,如果对象已经...

    Hibernate的Session的javadoc

    Hibernate的Session接口是Java应用程序与Hibernate之间主要的运行时交互接口,它提供了对持久化对象的创建、读取和删除操作。Session的概念是基于对象的状态管理和数据库事务的,它的生命周期通常与一个物理事务绑定...

    SSH笔记-管理Session和批量操作数据库

    3. **Hibernate Session管理**: - Hibernate的Session是与数据库交互的主要接口,它负责持久化对象的创建、加载、更新和删除。 - Session支持一级缓存,可以有效减少对数据库的直接访问,提高性能。 - 使用`...

    hibernate session详细方法,值得珍藏

    ### Hibernate Session 详解:值得珍藏的深度解析 在Java持久化领域,Hibernate无疑占据了举足轻重的地位,而`Hibernate Session`则是其中的核心组件之一,它提供了与数据库交互的主要接口,是对象/关系映射(ORM)...

    Hibernate_session_factory_配置.docx

    在Java的持久层框架Hibernate中,SessionFactory是核心组件之一,它是线程安全的,负责创建Session对象,每个Session对应于数据库的一次会话。配置SessionFactory主要是通过Hibernate的配置文件(通常为hibernate....

    HibernateSession , Transaction 研究

    本文将深入研究Hibernate中的`Session`和`Transaction`,这两个概念是理解Hibernate工作原理的关键。 首先,让我们了解`Session`。在Hibernate中,`Session`充当了应用程序与数据库之间的桥梁,它是持久化操作的...

    Hibernager_Session_Manager_ThreadLocal

    总结起来,"Hibernage_Session_Manager_ThreadLocal"是一个关于使用ThreadLocal在多线程环境中优化Hibernate Session管理的技术实践,通过这种方式可以提升应用程序的性能和安全性。`HibernateUtil.java`是实现这一...

    Spring hibernate opensessioninview

    综上所述,`OpenSessionInViewFilter`作为一种简化Hibernate Session管理和事务处理的方式,在适当的场景下能够极大地提高开发效率和代码质量。但在使用时也需要注意其潜在的性能影响和其他风险,合理选择是否启用该...

    hibernate02:session对象、hql查询、在线查询也叫对象查询、离线查询

    本话题主要聚焦于`Hibernate02`,涵盖`Session`对象、HQL(Hibernate Query Language)查询以及在线查询和离线查询的概念。 首先,我们来详细讨论`Session`对象。在Hibernate中,`Session`是应用程序与数据库之间的...

    hibernate--3.Hibernate数据持久化(通过 Session 操纵对象)

    本篇文章主要关注如何通过Hibernate的Session接口来操纵对象,实现数据的持久化。 首先,我们需要理解Session在Hibernate中的角色。Session是Hibernate的核心接口之一,它负责对象的持久化操作,比如保存、更新、...

    Hibernate Session 4种对象状态.docx

    在Java的持久化框架Hibernate中,对象的状态管理是其核心功能之一。Hibernate将对象的状态划分为四种:临时状态、持久化状态、游离状态和删除状态。这些状态反映了对象与数据库记录之间的关系,有助于理解Hibernate...

Global site tag (gtag.js) - Google Analytics