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 8151583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:26:01+00:00 2026-06-06T15:26:01+00:00

I am using hibernate in my project and it is well going, but my

  • 0

I am using hibernate in my project and it is well going, but my problem is i am little bit confused that is i am going good way for writing hibernate functions. Below i paste my block of code to understand how i had written the hibernate functions please check it and tell me whther it is a correct method. Here is my structure of block code

SessionFactory sessionFactory = 
                (SessionFactory) ServletActionContext.getServletContext().getAttribute(HibernateListener.KEY_NAME);
        Session hibernatesession = sessionFactory.openSession();
        try {
            hibernatesession.beginTransaction();
            // my database access will be here
            hibernatesession.getTransaction().commit();
            hibernatesession.flush();
            }
        catch(Exception e){
          hibernatesession.getTransaction().rollback();
          e.printStackTrace();
        }finally{
          hibernatesession.close();
        }

This is the structure that i have created for all my dao class functions, but now my website is loading very slowly. So my questions is the stricture i have used is a correct one. Is the above code causes to open multiple session hibernate at a time.

  • 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-06-06T15:26:02+00:00Added an answer on June 6, 2026 at 3:26 pm

    It is not good design to do transaction management in a DAO class. I would warmly recommend using Spring and its declarative transaction management, which is a breeze to set up and a no-brainer to use. If that is not available to you, then you should at least make a poor man’s alternative. One suggestion is to make each DAO a subclass of a class that has a method that does the boilerplate part (session open/close, transaction begin/commit) and calls out to an overridable method to do the real work. This would implement the Template pattern for your scenario. This is an outline:

    public abstract class DaoTemplate
    {
      public void execute() {
        final Session s = ((SessionFactory) ServletActionContext.getServletContext()
            .getAttribute(HibernateListener.KEY_NAME)).openSession();
        try {
          s.beginTransaction();
          doTheRealStuff();
          s.getTransaction().commit();
        }
        catch(Exception e) {
          s.getTransaction().rollback();
          e.printStackTrace();
        } finally { s.close(); }
      }
    
      protected abstract void doTheRealStuff();
    }
    

    Another suggestion, and something I actually used on a project ten years ago, is to implement the Strategy pattern. In that case you’d pass an object to execute that implements the method doTheRealStuff. Also, in that case the DaoTemplate class could be a singleton (a different name for the class would be appropriate then).

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

Sidebar

Related Questions

Well, my project was using EJB and JPA (with Hibernate), but I had to
I am using Hibernate and Spring on a small personal project. Well, still a
I am using hibernate as ORM tool. It's working properly, but the problem is
I am currently trying to solve a problem, that I have using Hibernate 4
I am a Java programmer and was working on a project using Hibernate and
I my project I am using hibernate (3.5.6) to interact with db.. I am
I'm using Hibernate and JPA for a small project. Somehow when trying to obtain
I am using hibernate, struts, extjs in my project. I have a Customer object
I'm using Gilead to persist my entities in my GWT project, im using hibernate
I'm working on a Spring MVC project in which I'm using Hibernate Validator to

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.