Hibernate SessionFactory openSession vs getCurrentSession vs openStatelessSession
来源:互联网 发布:沐猴而冠带 知小而谋强 编辑:程序博客网 时间:2024/05/18 02:26
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 –getCurrentSession()
, openSession()
and openStatelessSession()
.
Hibernate getCurrentSession
Hibernate SessionFactory
getCurrentSession() method returns the session bound to the context. But for this to work, we need to configure it in hibernate configuration file like below.
<
property
name
=
"hibernate.current_session_context_class"
>thread</
property
>
If its not configured to thread, then we will get below exception.
Exception in thread
"main"
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:
1012
)
at com.journaldev.hibernate.main.HibernateSessionExample.main(HibernateSessionExample.java:
16
)
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
HibernateSessionFactory
openStatelessSession() method returns instance ofStatelessSession
. There is another overloaded method where we can passjava.sql.Connection
object 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
package
com.journaldev.hibernate.main;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.StatelessSession;
import
com.journaldev.hibernate.util.HibernateUtil;
public
class
HibernateSessionExample {
public
static
void
main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
//Current Session - no need to close
Session currentSession = sessionFactory.getCurrentSession();
//open new session
Session newSession = sessionFactory.openSession();
//perform db operations
//close session
newSession.close();
//open stateless session
StatelessSession statelessSession = sessionFactory.openStatelessSession();
//perform stateless db operations
//close session
statelessSession.close();
//close session factory
sessionFactory.close();
}
}
- Hibernate SessionFactory openSession vs getCurrentSession vs openStatelessSession
- Hibernate getCurrentSession() VS openSession()
- openSession() VS getCurrentSession() in Hibernate
- openSession 与 getCurrentSession, openStatelessSession区别
- hibernate中的SessionFactory的openSession与getCurrentSession
- Hibernate3.1之后的openSession() VS getCurrentSession()
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- Hibernate里sessionFactory的openSession()和getCurrentSession()的区别
- Hibernate SessionFactory中openSession和getCurrentSession方法的区别
- (hibernate之一)Sessionfactory的getCurrentSession与openSession的区别
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- Hibernate中的opensession与SessionFactory.getcurrentSession原理机制
- Hibernate SessionFactory中openSession和getCurrentSession方法的区别
- hibernate中SessionFactory.opensession()与getcurrentsession()的区别
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- 【Hibernate】SessionFactory中的getCurrentSession()和openSession()的区别
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- hibernate的SessionFactory的getCurrentSession 与 openSession() 的区别
- HTTP方法中,哪个操作不是幂等的
- mysql学习笔记——显示&插入
- python类的使用2
- QT小结
- window调试学习2——事后调试
- Hibernate SessionFactory openSession vs getCurrentSession vs openStatelessSession
- SAP学习8--ABAP数据字典
- MFC中的ODBC(Sql Server)
- 《数据结构》第一章 绪论 问题收集站收集到的问题解答 分配
- Guava学习笔记:Google Guava 类库简介
- EPP3怎么安装SVN(EclipsePHP Studio 3.0)
- 用dumpdecrypted给App砸壳
- 一眼看清高科技,深度解析人工智能技术脉络
- .NET 判断内容是否在下拉框里