Design Pattern for database connection

Viewed 4988

Recently, I had an interview with cognizant. During the interview they asked me write a singleton design pattern, Which I did. After that they asked me 'ok, now tell us where you can use singleton ? ' I said 'to manage database connection'.

They were not satisfied with my answer and they asked ' ok, so can you manage 1 million users accessing your database with singleton pattern?' and I was speechless cause I didn't know the answer.

I want to know which design pattern is best for database connection? if i have to mange more than 10000 user connections to my database how can i do that?

I would really appreciate if you guys can help me with this topic.

Thank you

1 Answers

where you can use singleton ? where you would normally use a global. if it should be a named (or keyed) then use the multiton variant. As for the "best" design pattern for database connections, I would suggest the Object pool pattern. In the case of database connections specifically, an implementation of such is normally called a Connection Pool.

Related