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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:09:13+00:00 2026-05-23T15:09:13+00:00

I have implemented DAO as follows, //pseudoCode public MyDAO{ private Session session; MYDAO{ this.setSession(HibernateUtil.getSessionFactory().getCurrentSession());

  • 0

I have implemented DAO as follows,

//pseudoCode

public MyDAO{

  private Session session;
  MYDAO{
  this.setSession(HibernateUtil.getSessionFactory().getCurrentSession());
  }

  public void save(MyObj obj){
   //save obj in db
  }
}

Now i have used the dao to save a single object it works fine.Now if save two object inside seperate transcation i get a error saying “session is already closed”

EG

Feature feature = new Feature();
feature.setFeatureName("testf333");

FeatureDAO featureDAO = new FeatureDAO();
Transaction tx = featureDAO.getSession().beginTransaction();
featureDAO.makePersistent(feature);
tx.commit();
System.out.println("successfully save in db " + feature.getId());


tx = featureDAO.getSession().beginTransaction();  //getting error here
Feature feature4 = new Feature();
feature4.setFeatureName("4444");
featureDAO.makePersistent(feature4);

tx.commit();
System.out.println("successfully save in db " + feature.getId());

I think this problem can be solved by checking if session is closed .But where can i set a new session because i use the session from DAO to start a transcation

  • 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-23T15:09:14+00:00Added an answer on May 23, 2026 at 3:09 pm

    Rather than trying to hold onto the Session in your MyDAO, you’re probably best off holding onto just the SessionFactory, and getting a session from is when needed, by defining your getSession method in MyDAO as

    public Session getSession() {
       return sessionFactory.getCurrentSession();
    }
    

    or just save nothing related to session handling in MyDao at all and use

    public Session getSession() {
       return HibernateUtil.getSessionFactory().getCurrentSession();
    }
    

    A hibernate session is tied to a specific thread and closed on commit, but the session factory’s getCurrentSession() method gets a new session as needed so that you don’t have to worry about it.

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

Sidebar

Related Questions

I have implemented correctly bump's api, and added this code: - (void) configureBump {
We have implemented a typical DAO/Abstract Factory pattern. The design is like this: DAOFactory
I have a generic DAO class that looks like this: public class GenericDaoJpa <T
I have implemented DAO's based on Link in the HibernateDAOFactory class, there is a
Note: this question is a bit long I have a PHP-based system with Service-Dao-Model
Here is a class I have : public class ProxyDAO<T extends DAO<? extends Model>>
I have a core module with a class that contains: @Autowired private BaseDao dao;
I have a fully implemented DAO and all my beans inherit an Entity object.
I have a C# solution with this structure: Project 1: Contains a Dao (can
I have a generic interface: public interface DAO<T> { public T get(long id); public

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.