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

  • Compare date in SQL
    08 October

    How to Compare Dates in SQL

    Table of ContentsAgendaIntroduction to DATE in SQLComparing Dates in SQL with ExamplesUsing Comparison OperatorsUsing the BETWEEN ClauseComparing Dates for DATETIME Datatypes/FormatsUsing Comparison Operator with TimeStampCasting DATETIME as DATE without TimestampUsing Between Clause with DATETIME Agenda This article will look at yet another interesting topic in SQL commonly asked in interviews – How to Compare Dates […]

  • Get Number of Elements in Array in C++
    08 October

    Get Number of Elements in Array in C++

    Table of ContentsGet Number of Elements in Array in C++Using the sizeof() functionUsing the pointersUsing the size() functionGet the frequency of each element in array in C++Using nested for loopUsing maps One of the most fundamental collections in any programming language is an array. It stores a similar type of data at specific indices that […]

  • can you screenshot onlyfans
    08 October

    Can You Screenshot OnlyFans? Here is Everything You Need to Know

    Table of ContentsOnlyFans: A Brief IdeaCan You Screenshot OnlyFans?Can You Screenshot OnlyFans Without Getting Banned?Can You Screenshot OnlyFans Without Them Knowing: Screenshot NotificationsFrequently Asked Questions About: Can You Screenshot OnlyFans?What Happens When You Try Taking OnlyFans Screenshots?Is Screenshotting Illegal?Do the OnlyFans screenshots lead to a permanent account ban?Can OnlyFans screenshots be detected?Wrapping Up Can you […]

  • Trust browser enable
    08 October

    How to enable trust wallet browser

    Table of ContentsTrust DApp Browser: A Brief OverviewWhat are DApps on Trust Wallet?Access DApps Using Trust Wallet: Easiest ApproachTrust Browser Enable on Android: How to Activate/Enable DApp Browser on Trust WalletTrust Browser Enable on iOS: How to Activate/Enable DApp Browser on Trust Wallet#Method 1: “Trust Browser Enable” Procedure#Method 2: Enabling DApp by Visiting Pancake SwapFrequently […]

  • how-to-find-someone-on-onlyfans
    07 October

    How to Find Someone on OnlyFans: Working Methods

    Table of ContentsHow to Find Someone on OnlyFansHow to Find Someone on OnlyFansHow to Find Someone on OnlyFans: The Official Username MethodThe OnlyFans TrickHow to Find Someone on OnlyFans: The Email MethodHow to Find Someone on OnlyFans: The OnlyFinder MethodHow to Find Someone on OnlyFans By Location (Map Feature)How to Find Someone on OnlyFans By […]

  • How to delete snapchat messages the other person saved
    07 October

    How to delete snapchat messages the other person saved

    Table of ContentsHow to delete Snapchat messages the other person saved#Step 1: Log in to Your Snapchat Account#Step 2: Navigate to the Desired Chat#Step 3: Unsave The Message from Your End#Step 4: Long-Press and Delete the Message#Step 5: Proceed with the Learn More option#Step 6: Click Okay#Step 7: Tap on the option that says “Delete.”How […]

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.