A singleton pattern has nothing to do with users or sessions. (If it is somehow tied to these concepts then it is being misused.)
The singleton does have to do with its visibility and useability to other classes in the solution and for holding and managing a db connection it is actually quite useful as long as you use it properly.
One of the main points of using a singleton is that you don't keep creating extra resources because the singleton manages that internally. So you create the database connection the first time the singleton is accessed and then offer the same connection after the fact.
The thing to be careful with is to make sure that you don't abuse the accessibility of storing the db connection in this way. Make sure you're following best practices and patterns to only use the db connection from a DAL (data access) layer or Repository or other data model provider.
If you find yourself calling the db singleton from views you'll need to redesign your pattern.
Main Topics
Browse All Topics





by: MuhammadKashifPosted on 2009-10-06 at 05:00:41ID: 25504005
singleton pattern should not be used for database connection. because connection should be created perform tasks and it should be closed.
singleton pattern should be used where you are sharing some information to all the users. the best example for singleton pattern implementation is the hit counter of a web site; which keeps incrementing for every user visiting the site.