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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:07:06+00:00 2026-05-26T06:07:06+00:00

The webapp uses Spring’s OpenSessionInViewInterceptor to open a Hibernate session for each request, closing

  • 0

The webapp uses Spring’s OpenSessionInViewInterceptor to open a Hibernate session for each request, closing it at the end of request processing. This works very well. However some requests like file downloads can last a long time, keeping the session open and the db connection tied up. In our case, copying the file data to the response output stream is the last thing the controller does, and doesn’t require the Hibernate session (the file data is read from the server filesystem). These file download requests can quickly consume most of the connections in our db connection pool.

I’ve given the controller a reference to the SessionFactory, and invoke the following method just before copying the file data to the response output stream:

SessionFactoryUtils.closeSession(SessionFactoryUtils.getSession(sessionFactory, false));

OpenSessionInViewInterceptor does the same thing later in the request process, and closing the session appears to be idempotent (i.e. calling this method multiple times has the same effect as calling it once). In other words, local testing of this change went well. But it seems a little dangerous; if OpenSessionInViewInterceptor or SessionFactoryUtils change in later versions of Spring, this approach might break in subtle ways (possibly leaking connections?).

The end goal is to close the database connection before starting to send the file data, and this is one way to achieve that. Another solution would be the controller putting the input stream into the request as attribute, and have a filter (firing after the OpenSessionInViewInterceptor) copy the input stream’s data to the response. This has its own set of challenges (e.g. the relevant code is now split between the controller and the filter, instead of being centralized in the controller).

Is there a better way to handle this?

  • 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-26T06:07:07+00:00Added an answer on May 26, 2026 at 6:07 am

    One option would be to not use OSiV for controllers/actions returning a file. Another would be to create your own version that you had more confidence in (after all, it’s not terribly complex).

    I doubt that you’d leak connections if the interceptor/utils changed. I’m not excited about the idea of having something that returned the file after the interceptor has done its thing; that implies having some dangling state picked up by a follow-up interceptor (or filter, although IMO doing it in a chained interceptor is probably cleaner) and it strikes me as clunky and error-prone.

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

Sidebar

Related Questions

I am working on a webapp which uses Spring security 2.0 (not the new
I am working on a webapp that uses both the Spring Framework and RESTEasy.
I have an existing java webapp that uses Hibernate for it's persistence. I've been
So I've got a webapp that uses Dojo and Spring. I've got some grids
My project uses tapestry 5.3.0,spring 3.0.6.RELEASE,hibernate 3.6.8.FINAL and maven.When I intruct jetty to start
I have a webapp that uses Forms Authentication using ActiveDirectoryMembershipProvider. That works fine... users
I am developing a webapp that uses Spring Security as security layer. One important
I have a webapp running in Tomcat which uses Spring for dependency injection. (It's
I have a java/spring webapp which uses HibernateTemplate and I was wondering if it
Consider the following implementation of Web app and Client - that uses Spring/Hibernate for

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.