Last post Jul 05, 2013 09:33 AM by Illeris
Jul 04, 2013 12:32 AM|ramana123|LINK
Hi, got a question in Singleton pattern.
I am adding lot of configuration info into singleton class which will fetch the configuration datza from data base(I kept in data base for various reasons as its easy to update as and when needed like configuration URL, user name, pwd,path etc).
I know singleton will issue only one object (coded in such way with lock etc) for applciation life time but confused , that means one single object for each user session? or single object for all users?
Also, since the data is coming from data base into singleton, will my user sees the latest values when I update the configuration in data base while user logged into the app(using the app) or does he need to re-login to get latest config data?
Jul 04, 2013 12:53 AM|Mikesdotnetting|LINK
It depends on how you create your singleton. If you create it is in the context of a session, it will only be available per user. If you create it as an application level variable it will be avaialble to all users.
If you are likely to change values of an application level variable during the life of the application, you should add some code to reset the app level variable when you update values in the database. Although if you do this, you could end up with problems.
Ideally, you should not store values that are likely to change in this way. And if you do change them, you should shut the app down. If someone is logged in and doing something with a path that suddenly changes, that is likely to result in file not found errors
Jul 04, 2013 09:46 PM|magicmike2011|LINK
After reading your case I just wanted to weigh in a possible solution that may benefit your scenario.
Rather than implementing a singleton pattern (which are rife with issues, especially in web-farm/cloud scenarios), consider using a ReaderWriterLockSlim to provide locking on the object when required.
The ReaderWriterLockSlim allows for multiple concurrent reads but locks writiing to only one write at a time (which would be perfect for config data in your scenario). It uses a queue mechanism to enforce locking without disrupting requests and it quite
link to the MSDN page for the class and a good talk about it:
Jul 05, 2013 09:33 AM|Illeris|LINK
I assume we are dealing with a web app: