Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3304208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:02:16+00:00 2026-05-17T21:02:16+00:00

I am new in hibernate,after read the hibernate api and tutorial,it seems that the

  • 0

I am new in hibernate,after read the hibernate api and tutorial,it seems that the session should closed when not used.

Like this:

Session sess=getSession();
Transcration tx=sess.beginTranscration();
//do something using teh session
sess.save(obj);
tx.commit();
sess.close;

I have no question when using it in a standlone application.
However I am not sure when using in the web app.

For example, I have a servlet: TestServlet to receive the parameters from the client, then I call a Manager to query something according to the parameters, just like this:

class TestServlet{
  doGet(HttpServletRequset,httpServletResponse){
    String para1=request.getParam...();
    String para2=.....
    new Manager().query(para1,para2);
  }
}

class Manager{
  public String query(String pa1,String pa2){
    Session=....// get the session
    //do query using para1 and 1
    session.close() //Here, I wonder if I should close it.
  }
}

Should I close the session in the query method?

Since someone told me that session in hibernate is just like the connection in jdbc. So opening and closing it so frequently is the correct way?

BTW, does the tx.commit() is required each time?

Also what’s the thread problem about using session in servlet, since I saw the session is not thread safe in api.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:02:16+00:00Added an answer on May 17, 2026 at 9:02 pm

    I am new in hibernate,after read the hibernate api and tutorial,it seems that the session should cloesd when not used.

    It should be closed when you’re done with (but this can be done automatically for you as we’ll see).

    I have no question when using it in a standalone application. However I am not sure when using in the web app.

    Well, as explained in the section 11.1.1. Unit of work of the documentation, the most common pattern in a multi-user client/server application is session-per-request.

    For example, I have a servlet:TestServlet to recieve the parameters from the client,then I call a Manager to query something according to the parameters: just like this (…) Should I close the session in the query method?

    It all depends on how you obtain the session.

    • if you use sessionFactory.getCurrentSession(), you’ll obtain a “current session” which is bound to the lifecycle of the transaction and will be automatically flushed and closed when the transaction ends (commit or rollback).
    • if you decide to use sessionFactory.openSession(), you’ll have to manage the session yourself and to flush and close it “manually”.

    To implement a session-per-request pattern, prefer the first approach (much easier and less verbose). Use the second approach to implement long conversations.

    The wiki page Sessions and transactions is a good complement to the documentation on this topic.

    BTW, does the tx.commit() is required each time?

    You might want to read Non-transactional data access and the auto-commit mode to clarify a few things but, to put it simply, your Hibernate code has to be executed within a transaction and I’d suggest to use explicit transaction boundaries (i.e. explicit beginTransaction and commit).

    Also what’s the thread problem about using session in servlet, since I saw the session is not thread safe in api.

    Just don’t make it an instance variable of the Servlet and you won’t have any problem.

    References

    • Hibernate Core 3.3 Reference Guide
      • Chapter 11. Transactions and Concurrency
    • Hibernate wiki
      • Sessions and transactions
      • Non-transactional data access and the auto-commit mode
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Warning - I am very new to NHibernate. I know this question seems simple
Please excuse my ignorance on the topic, as I am relatively new to Hibernate
public static IList<T> LoadObjectListAll<T>() { ISession session = CheckForExistingSession(); var cfg = new NHibernate.Cfg.Configuration().Configure();
New to javascript/jquery and having a hard time with using this or $(this) to
I'm new to PHP and also with Doctrine. (Worked before with Hibernate ORM implementation).
I am having this problem.. using (var transaction = Session.BeginTransaction()) { var t =
I just started using Hibernate on a new project, and with my first entity,
I had set up hibernate plugin to NetBeans and can not connect to MySQL
<prop key=hibernate.hbm2ddl.auto>create</prop> creates a new database schema and <prop key=hibernate.hbm2ddl.auto>update</prop> create if it is
i'm fairly new to NHibernate and although I'm finding tons of infos on NHibernate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.