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

  • Home
  • SEARCH
  • 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 6143175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:29:19+00:00 2026-05-23T18:29:19+00:00

Intro I’m bulding a REST web service using Hibernate and Jersey to supply JSON

  • 0

Intro

I’m bulding a REST web service using Hibernate and Jersey to supply JSON data to mobile clients. I have a general question about how to deal with Hibernate sessions internally. There are two different approaches(A,B) I’d like to discuss.

Approaches

A. Someone told me I should open a new session per user, let it open for the whole web session of the user and finally close this session after the user stopped using my web service. I was told it would be a better approach from the view of security and performance.

Though I’ve read that:

"Sessions are irrelevant" Common REST Mistakes, 6. 

B. Right now I’m using a SessionFactory to open a session in my service classes and close this session immediately after a query is done. My web service is only using GET and POST requests. There is not PUT or DELETE. I don’t need any user authentication (like oAuth) to request data. Therefore I don’t think it’s necessary to use Transactions

Here is an Example of my Service class:

Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction(); 

Query query = session.createQuery("from RoomEntity");   
@SuppressWarnings("unchecked")
List<RoomEntity> list = (List<RoomEntity>) query.list();

session.close();

Questions

Would be great to hear your opinion about my following questions:

  1. What would be the best practice? Which approach do you follow?
  2. What do you think about the performance matter?
  3. What do you think about the security matter?
  • 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-23T18:29:19+00:00Added an answer on May 23, 2026 at 6:29 pm

    I don’t like A. It makes your service stateful and how do you know when a user has finished with your services anyway?

    The general rule with Hibernate and web apps is to use a single session per http request. Most REST service GET requests map pretty simply to a single DB query as you have demonstrated with B, so this is the way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

INTRO: I created a java application using JFrame. I have a JMenuBar at the
Intro I work in a facility where we have microscopes. These guys can be
Intro: EDIT: See solution at the bottom of this question (c++) I have a
Intro: Web application, ASP.NET MVC 3, a controller action that accepts an instance of
Brief intro about my requirement. I have an empty JSF dataTable. Now, when I
Content type Events has intro, body and an event date field (using Date module).
Intro Consider you have a list of key/value pairs: (0,a) (1,b) (2,c) You have
Intro I have an object @organization that has_many :quick_facts Basically, I want to produce
I've created an intro animation to a page I'm working on using jQuery and
I'm in an intro level programming class and I have to make a grade

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.