hibernate通过sessionFactory有两种方式获取session:一种是openSession,一种是getCurrentSession
简单区别
1、openSession创建session时autoCloseSessionEnabled参数为false,即在事物结束后不会自动关闭session,需要手动关闭,如果不关闭将导致session关联的数据库连接无法释放,最后资源耗尽而使程序当掉。 2、getCurrentSession创建session时autoCloseSessionEnabled,flushBeforeCompletionEnabled都为true,并且session会同sessionFactory组成一个map以sessionFactory为主键绑定到当前线程。
3、现在对hibernate两种创建session方式进行详细描述:
3.1入口:
private static SessionFactory buildSessionFactory() {
try {
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration
” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration
“.
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
spring3.0.6和hibernate3.6.7集成: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean进行初始化,里面调用的是new AnnotationConfiguration().configure().buildSessionFactory();
下面的文章将对spring加载配置hibernate进行初始化进行详细分析
分享到:
相关推荐
当我们调用SessionFactory().getCurrentSession()时,Hibernate会为我们提供一个已存在的或者新创建的Session实例,这个行为与直接调用SessionFactory.openSession()有所不同。`getCurrentSession()`方法旨在支持...
在Java的Hibernate框架中,`getCurrentSession()` 和 `openSession()` 都是用于获取与数据库交互的Session对象,但它们之间存在显著的区别。理解这些差异对于优化数据访问性能和管理事务至关重要。 首先,`...
理解 `SessionFactory.getCurrentSession()` 和 `Session.openSession()` 的区别对于优化和正确使用Hibernate至关重要。 首先,`SessionFactory` 是Hibernate中的一个接口,它是一个全局的、线程安全的工厂类,用于...
2. **为什么使用getCurrentSession()**:与直接调用`openSession()`创建新的Session相比,`getCurrentSession()`有以下优势: - 它能够自动管理Session的生命周期,比如在请求结束时关闭Session,避免资源泄露。 -...
同时,还探讨了Hibernate框架中getCurrentSession()与openSession()的区别。 首先,要操作数据库中的Clob字段,需要关注的是如何在Java对象与Clob字段之间进行转换。由于Clob字段通常用于存储大量文本数据,如果...
- 错误2:`java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session` - 解决方案:由于Hibernate4移除了HibernateDaoSupport,需要改写DAO层,直接使用Hibernate...
### Hibernate框架技术总结 #### 一、概述 Hibernate是一个开源的对象关系映射(ORM)框架,它为Java...通过这些内容的学习,开发者可以更好地理解和掌握Hibernate的工作原理及应用场景,提高开发效率和代码质量。
实体映射是Hibernate的核心功能之一。这部分内容主要涉及如何使用XML映射文件和注解的方式来进行实体映射。 **XML映射文件示例:** - **Student.hbm.xml** 文件用于定义`Student`类与数据库表之间的映射关系。 - ...
11. Hibernate中getCurrentSession和openSession的区别是什么? `getCurrentSession`是用于在已有的事务上下文中获取或创建Session,确保与当前事务的一致性。而`openSession`则是直接创建一个新的Session,不考虑...
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
编程式事务处理是手动管理事务边界,而OpenSession/GetCurrentSession模式则是在操作数据库时自动开启和关闭Session。这两者在事务管理和性能上有不同的考量。学习者将了解到何时选择哪种方式,以及它们在Spring中...
- 获取Session:通过SessionFactory的openSession()或getCurrentSession()获取Session实例。 - 开启事务:在Session上开始一个新的数据库事务。 - 对象操作:执行增删改查操作,如save()、update()、delete()、...
本文详细介绍了 Hibernate 中 Session 的关闭实例解析,包括 getSession() 和 openSession() 的区别、getCurrentSession() 的配置、openSession() 和 getCurrentSession() 的关联,以及 Session 的关闭。希望本文...
在 Hibernate 中,登录页面通常涉及以下关键组件和概念: 1. **实体类(Entity Class)**: 这是与数据库表相对应的Java类。例如,可以创建一个名为`User`的实体类,包含用户名(username)和密码(password)属性,...
openSession 和 getCurrentSession 是 Hibernate 中的两个会话方法,openSession 用于创建新的会话,getCurrentSession 用于获取当前会话。 saveOrUpdate() 和 merge() saveOrUpdate() 和 merge() 是 Hibernate 中...
在Spring中,我们可以使用SessionFactory的openSession()或getCurrentSession()方法获取Session实例。 4. **事务管理**: Spring提供PlatformTransactionManager接口,可以用来管理数据库事务。在Hibernate整合中,...
8. getCurrentSession()和openSession()的区别: - getCurrentSession()会检查当前线程是否已有Session,若有则返回,若无则创建。 - openSession()方法总是创建一个新的Session。 - getCurrentSession()在事务...
在Spring中,我们可以使用`SessionFactory.openSession()`或`SessionFactory.getCurrentSession()`方法获取Session实例。 4. **事务管理**:Spring提供了声明式事务管理,只需在方法上添加@Transactional注解,即可...
`SessionFactory` 的 `openSession()` 方法用于创建新的 `Session`,而 `getCurrentSession()` 方法则根据上下文获取或创建 `Session`,确保在同一个事务中使用相同的 `Session`。 `Session` 类是与数据库进行交互...