0

When using Singleton scope in an MVC application, would other threads be blocked from accessing my singleton object while one thread is already calling a method on it?

Eg: I have a repository that is bound in singleton scope. If multiple users visit a page that uses the repository to fetch data off the database, does it mean that the until the first thread that acquired the singleton instance is finished doing its work, the second thread cannot obtain the singleton instance?

In which case, if my repository takes 2 - 3 seconds to load up data from the db, does it mean the other thread is waiting to obtain this singleton object?

Thanks

1 Answer 1

1

No. Ninject only makes sure that everyone gets the same object. Handling concurrent access to the object is your (or the object's implementation) responsiblity.

However, if the singleton's constructor takes a long time, it will take a long time to instanciate the first object where the singleton will be injected as constructor argument.

It is generally inadvisable to have costly (long-running) code in the constructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.