Hibernate SessionFactory openSession vs getCurrentSession vs openStatelessSession
2014-09-23 10:35
656 查看
Hibernate SessionFactory is the factory class through which we get sessions and perform database operations. Hibernate SessionFactory provides three methods through which we can get Session object –
If its not configured to thread, then we will get below exception.
Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed. Hibernate Session objects are not thread safe, so we should not use it in multi-threaded environment.
We can use it in single threaded environment because it’s relatively faster than opening a new session.
StatelessSession does not implement first-level cache and it doesn’t interact with any second-level cache. Since it’s stateless, it doesn’t implement transactional write-behind or automatic dirty checking or do cascading operations to associated entities.
Collections are also ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors. It’s more like a normal JDBC connection and doesn’t provide any benefits that come from using hibernate framework.
However, stateless session can be a good fit in certain situations, for example where we are loading bulk data into database and we don’t want hibernate session to hold huge data in first-level cache memory.
A simple program showing these methods usage is given below.
HibernateSessionExample.java
That’s all for different session factory methods that we can use to obtain session object.
getCurrentSession(),
openSession()and
openStatelessSession().
Hibernate getCurrentSession
HibernateSessionFactorygetCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file like below.
If its not configured to thread, then we will get below exception.
Since this session object belongs to the hibernate context, we don’t need to close it. Once the session factory is closed, this session object gets closed. Hibernate Session objects are not thread safe, so we should not use it in multi-threaded environment.
We can use it in single threaded environment because it’s relatively faster than opening a new session.
Hibernate openSession
HibernateSessionFactoryopenStatelessSession() method returns instance of
StatelessSession. There is another overloaded method where we can pass
java.sql.Connectionobject to get a stateless session object from hibernate.
StatelessSession does not implement first-level cache and it doesn’t interact with any second-level cache. Since it’s stateless, it doesn’t implement transactional write-behind or automatic dirty checking or do cascading operations to associated entities.
Collections are also ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors. It’s more like a normal JDBC connection and doesn’t provide any benefits that come from using hibernate framework.
However, stateless session can be a good fit in certain situations, for example where we are loading bulk data into database and we don’t want hibernate session to hold huge data in first-level cache memory.
A simple program showing these methods usage is given below.
HibernateSessionExample.java
That’s all for different session factory methods that we can use to obtain session object.
相关文章推荐
- hibernate的openSessionFactory和getCurrentSession
- this.getsession和this.getHibernateTemplate().getSessionFactory().getCurrentSession()/OpenSession区别 .
- Hibernate getCurrentSession() VS openSession()
- Hibernate openSession() 和 getCurrentSession的区别
- 解决hibernate产生的id序列或者setXX不能同步到数据库到问题(this.hibernateTemplate.flush();hibernateTemplate.getSessionFactory().getCurrentSession().connection().commit())
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- hibernate session.open()和session.getCurrentSession()区别很重要
- this.getsession和this.getHibernateTemplate().getSessionFactory().getCurrentSession()/OpenSession区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate openSession和getCurrentSession
- hibernate之openSession 和 getCurrentSession 概述
- this.getsession和this.getHibernateTemplate().getSessionFactory().getCurrentSession()/OpenSession区别
- hibernate openSession() 与 getCurrentSession()
- hibernate openSession()与getCurrentSession()的区别
- sessionFactory.getCurrent()和sessionFactory.openSession()的区别
- Hibernate学习04---Hibernate对象三种状态以及openSessoin/getCurrentSession
- hibernate openSession and getCurrentSEssion
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate整合进spring--使用hibernateTemplate.getSessionFactory().getCurrentSession()理解
- getHibernateTemplate和getSession 区别, this.getHibernateTemplate().getSessionFactory().getCurrentSession()和OpenSession区别