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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:09:47+00:00 2026-05-20T08:09:47+00:00

Application is based on Spring 2.5.5 and hibernate 3.2.0 GA. I have the following

  • 0

Application is based on Spring 2.5.5 and hibernate 3.2.0 GA.

I have the following method in my DAO that gets MessageEntities attached to the specified User:

public MessageEntity findByUserId(int userId) {
      List<MessageEntity> result = (List<MessageEntity>) em.createNamedQuery(MessageEntity.Q_BY_USER_ID).setParameter("userId", userId).getResultList();
      if (!result.isEmpty()) {
         return result.get(0);
      } else {
         return null;
     }
}

I need to call this method from my integration test to check whether system’s behaviour is valid. As long as this method is not transactional, all I get is org.hibernate.SessionException: Session is closed!. The easiest way to avoid this is to mark findByUserId method with @Transactional(readOnly = true). But as I understand, transaction management should be the duty of service tier to avoid unnecessary transactions creation. So, my question is: how can I properly get away from SessionException?

  • 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-20T08:09:48+00:00Added an answer on May 20, 2026 at 8:09 am

    You need to perform all your database actions within a transaction scope. As you identified its usually considered good design to let the service layer of your database model deal with transactions. The only constraint then becomes that you must invoke your service model to get within the transaction scope, which might be undesirable during test.

    I would recommend to make use of the testing fascilites provided by spring. See 9.3.2.3 Transaction management

    You could also manually create a transaction before testing your method, e.g., by

    Session sess = factory.openSession();
    Transaction tx = null;
    // try catch 
    tx = sess.beginTransaction();
    findByUserId(userId);
    tx.commit();
    tx.rollBack();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is based on Swing, Spring 2.5.2, Hibernate 3.3.1. If
I have a Spring MVC based Web Application with Hibernate. Following is the directory
I have a localized spring mvc based web application, that has an externalized messages
We (will) have the following architecture: Base.war will be a self-contained spring-hibernate application All
I have web application based on Spring-MVC. I've integrated it with Hibernate 3.6.0. When
We have a Java web application that uses Spring and Hibernate and has a
I have a small Spring web application, with the typical MVC Service DAO JPA/Hibernate
I have a web application that uses spring and hibernate for JPA support, but
I am using Spring/Hibernate and Spring-Security for my web-based application. Now I have a
I am building a spring based application that handles server-side game management for a

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.