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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:26:32+00:00 2026-06-10T04:26:32+00:00

Assume the following class is a singleton public class myDAO { //determines the tableName

  • 0

Assume the following class is a singleton

public class myDAO {
    //determines the tableName and Id column names to based on itemType
    private String itemType;
    public void setItemType(String itemType) {...} 

    //Following methods use itemType to customize their queries
    public boolean findObj(int id){...} 
    public boolean deleteObj(int id){...}
    public boolean updateObj(Obj obj){...}
    //etc...
}

The code was recently refactored with the setter, thus giving the DAO some state. I’m seeing that this is configured as singleton-scope in Spring’s config file. I have a strange feeling that this could lead to potential race conditions. Is that correct?

I’m really not sure if this is indeed the case but it’d be a nightmarish situation to figure it out, IMHO if it were true. I know this may not be the best possible design but I just wanted to know if this could lead to race conditions when concurrent threads are querying for different itemTypes. I was thinking of changing the scope to prototype but am not sure if it’s indeed required in this case.

  • 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-10T04:26:33+00:00Added an answer on June 10, 2026 at 4:26 am

    If multiple threads are calling setItemType(...) and then calling findObj(...) and are expecting to find the object with an id and with the item-type then yes, your strange feeling is correct, this is a potential race condition and won’t work in a multi-threaded application.

    You should instead pass in the itemType to each of your methods:

    public boolean findObj(String itemType, int id){...} 
    public boolean deleteObj(String itemType, int id){...}
    public boolean updateObj(String itemType, Obj obj){...}
    

    If the itemType is set just once after the DAO has been constructed by Spring then this is okay but should be done as part of your Spring configuration which is done single-threaded.

    If you have to add an itemType argument to all or most of the methods then it seems to me that you no longer should have a singleton. Instead you should consider having a DAO factory or something and have multiple DAO instances, each with their own item-type.

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

Sidebar

Related Questions

Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Let's assume I have the following code: public class MainClass { public static void
Assume the following class: class Person { public string FirstName {get;set;} public string LastName
Assume the following code: public class Foo { public string Bar { get; set;
Let's assume following code snippet: public class NotThatWellWrittenClass { public static void doSmth() {
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
Assume the following trivial class: package Sample; public class Status { private Long id;
We have the following class public class TemporalData<T> { private T data; private String
Assume the following class public class TestObject{ public void synchronized method1(){ //some 1000 lines
Assume the following: we have class B, which is a private class nested inside

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.