KBusc
KBusc

Reputation: 663

Possible Race Condition in Singleton Bean

I am running into what appears to be a race condition in a Spring bean that is called from a third party library and I am trying to determine if there is an issue with my implementation or if the issue is possibly with the third party library

@Override
public InputObject addMessageToInput(InputObject input) {

    List<String> messages = doStuff();

    log.debug("Messages are {}", messages);//messages appear to be correct at this point

    doLongRunningOpertation(); 

    log.debug("Messages are {}", messages);//messages appear to be correct at this point
    input.addMessages(messages);

    return input; //Return to third party library here
}

Upon return of this function the third party library handles displaying messages to the front end. The issue that I am seeing is that the messages displayed on the front end sometimes include extra or fewer messages than they should. I should also note that the program is NOT multi threaded.

Is there anything I am missing in my code that could be causing this race condition?

Upvotes: 2

Views: 301

Answers (1)

hooknc
hooknc

Reputation: 4991

Well, there can be lots of things happening, but I think your code looks pretty ok.

Somethings to consider...

Does the messages list get passed around at all in the doLongRunningOperation() method? If so, is it being placed into a class variable at anypoint?

Is the InputObject a new object every time it is passed into your code? If not, then that could cause issues.

Is the InputObject messages variable empty when it comes into your code?

You could consider returning a new instance of InputObject instead of using the instance that is sent to your method.

You could comment out the long running process to see if the problem keeps happening.

If you can see the problem repeat in your particular development environment you could put a debug breakpoint in the InputObject.addMessages() method and see when it is getting called.

Just some ideas to think about and perhaps try to help debug the problem.

Upvotes: 2

Related Questions