Hibernate SessionFactory openSession vs getCurrentSession vs openStatelessSession

本文深入探讨了Hibernate Session Factory的核心概念及其如何通过getCurrentSession(), openSession()和openStatelessSession()方法获取Session对象。重点阐述了当前会话对象的特点、配置注意事项以及在单线程环境中的使用优势。同时,对比了StatelessSession对象的特性,包括其不支持第一级缓存、不与第二级缓存交互等特性,并提供了示例代码展示如何在实际项目中运用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Hibernate SessionFactory openStatelessSession() method returns instance of StatelessSession. There is another overloaded method where we can pass java.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();
         
    }
 
}
That’s all for different session factory methods that we can use to obtain session object.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值