Difference between openSession and getCurrentSession in Hibernate

You might know there are two ways to create or get session in hibernate. We have below two methods in SessionFactory class to create a session.

Tutorial Content:

  • openSession
  • getCurrentSession
Now we will see differences between openSession and getCurrentSession.

openSession

When you call SessionFactory.openSession, it always create new Session object afresh and give it to you. You need to explicitly flush and close these session objects. As session objects are not thread safe, you need to create one session object per request in multithreaded environment and one session per request in web applications too.

getCurrentSession

When you call SessionFactory. getCurrentSession, it will provide you session object which is in hibernate context and managed by hibernate internally. It is bound to transaction scope.
When you call SessionFactory. getCurrentSession , it creates a new Session if not exists , else use same session which is in current hibernate context. It automatically flush and close session when transaction ends, so you do not need to do externally.
If you are using hibernate in single threaded environment , you can use getCurrentSession, as it is faster in performance as compare to creating  new session each time.
You need to add following property to hibernate.cfg.xml to use getCurrentSession method.
If you do not configure above property, you will get error as below.

openSession vs getCurrentSession :

Lets summarise differences between openSession and getCurrentSession in below table
Parameter
openSession
getCurrentSession
Session object
It always create new Session object
It creates a new Session if not exists , else use same session which is in current hibernate context
Flush and close
You need to explicitly flush and close session objects
You do not need to flush and close session objects, it will be automatically taken care by Hibernate internally
Performance
In single threaded environment , It is slower than getCurrentSession
In single threaded environment , It is faster than getOpenSession
Configuration
You do not need to configure any property to call this method
You need to configure additional property “hibernate.current_session_context_class” to call getCurrentSession method, otherwise it will throw exceptions.

Related Posts

  • nvarchar vs varchar
    29 July

    nvarchar vs varchar in SQL Server

    Table of ContentsIntroductionVARCHARNVARCHARExamples to Demonstrate Difference between VARCHAR and NVARCHARVARCHAR vs NVARCHAR Comparison Chart This article will look at the difference between the data types NVARCHAR and VARCHAR in SQL Server. These data types are commonly used for storing textual data or strings for various design purposes. We will look at the description of each […]

  • Stream2watch
    29 July

    Stream2watch alternatives -13 Best Websites Like Streamtowatch in 2021

    Table of ContentsBest Working Proxies of the Stream2watch in 2021Stream2Watch alternativesSport365SportP2PCricFree12th PlayerVIP BoxLiveTVWiziwigStopStreamSport LemonMamaHDLAOLA1Sport RARTime4TVfuboTVFirstRowSportsVIP LeagueFrequently Asked Questions**Is Stream2watch a safe website?What are the best Streamtowatch Alternatives?Do you need an account for Stream2watch?Final Words Are you after the best Stream2watch alternatives? Good News! this article is all about efficient web-based resources for streaming football, baseball, […]

  • 29 July

    AirPod Case not Charging – Common Issues and Fixes

    Table of ContentsBefore you StartWhy Is My AirPod Case Not Charging?Five Easy Solutions for AirPods Case Not Charging1. Reset Your AirPods Case2. Clean the Charging Port 3. Get a new Charger and Cable4. Update the AirPods Firmware5. Replace the CaseTips for Avoid AirPod not Charging IssueFrequently Asked QuestionsWhy isn’t my AirPod case charging, but my AirPods […]

  • Mobdro alternatives
    29 July

    Mobdro Alternatives – 14 Best apps like Mobdro in 2021

    Table of ContentsBest Mobdro Alternatives Working in 2021Cyberflix TVMorph TVTerrarium TVTea TVVisit Thop TVTubi Tv123MoviesMovie BoxVuduOreo TVMegaBox HDFreeFlixYuppTVFrequently Asked QuestionsWhat is Mobdro?Why do you need Mobdro alternatives?Are there free Mobdro alternatives?Wrapping Up Can’t access Mobdro anymore and are worried about watching your favorite movies? Take a breath; you still have a long list of working […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.