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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:31:22+00:00 2026-05-27T10:31:22+00:00

I’ve been learning how to use the Spring framework for hibernate transaction management, and

  • 0

I’ve been learning how to use the Spring framework for hibernate transaction management, and so far it has been a great help for that purpose. The problem is that recently, I realized that I hadn’t been giving much thought to how the pattern that I chose handles concurrency, specially in the case of a web application.

Below is the code that exemplifies the pattern that I’m using, which is a combination of some examples I found, and a custom servlet implementation. I have a few doubts about how this pattern works and whether it is thread-safe since I customized it a bit. Some of my concerns are:

  • Even though servlets are technically not singletons, I’m under the impression that most of the time there will be only a single instance for each servlet class.
  • If I understand correctly, each auto-wired property will be a singleton, so if the above its true, then there’ll be a single servlet instance with a single service instance, which in turn has a single DAO instance, which has its own SessionFactory instance.
  • If the above is correct then I can imagine that every request on the servlet will be using the same objects and I wonder whether this is a good thing. After giving it some thought I can’t tell whether it would be better for thread-safety to have singletons or to have new instances created for each http request.
  • Usually these spring patterns have a @Controller class which I skipped in favor of our custom servlet and I’m wondering whether that could break the concurrency of the pattern somehow.

Thanks

public interface UserDAO
{
    public void save(User user);
}

@Repository
public class HibernateUserDAO implements UserDAO
{
    @Autowired(required=true)
    protected SessionFactory sessionFactory;

    public void save(User user)
    {
        this.sessionFactory.getCurrentSession().save(user);
    }
}

public interface UserService
{
    public void saveUser(User user);
}

@Service
public class DefaultUserService implements UserService
{
    @Autowired(required=true)
    private UserDAO userDAO;

    @Transactional
    public void saveUser(User user)
    {
        this.userDAO.save(user);
    }
}

public class UserServlet extends CustomServlet
{   
    @Autowired(required=true)
    private UserService userService;

    public void init() throws ServletException
    {
        super.init();
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        //would call the appropriate method depending on parameters, etc.
        //simplified here for demonstration purposes
        this.userService.saveUser(user);
    }
}
  • 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-27T10:31:23+00:00Added an answer on May 27, 2026 at 10:31 am

    If you have a suitable strategy set up in the Hibernate configuration, then sessionFactory.getCurrentSession() will return a scoped session, not a global one. Session factories are intended to be singletons, that’s quite correct; getCurrentSession() is the usual method to bridge to the current context scope.

    “suitable strategy” varies, but if you’re using the spring transaction management wrapped around your DAO, then a session will have a lifetime of a DAO method call, and Spring will orchestrate calling commit/rollback as appropriate on exit.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.