2 ms·
I must admit I can't remember when I have last seen it. I guess the usual case is the ConnectionPool for the database, which for most Java Web projects is being
by Tichy 19y ago
I must admit I can't remember when I have last seen it. I guess the usual case is the ConnectionPool for the database, which for most Java Web projects is being handled by the container (not sure how Rails does it). But in that case it does not seem the end of the world to have two connection pools. Somehow you have to still manage the whole picture, though... Maybe two are OK, but what if suddenly you have 1000 connection pools? Maybe then suddenly you start wishing you had restricted them to 1 from the start.
How is a global variable different from a singleton, anyway? Seems to me, in distributed and multithreaded environments the singleton problem is still a hard problem.
- sah 19y agoConnectionPool is an excellent example of a bad case for Singleton: what if you have two databases? What if you want to use different pools for different types of queries? A global variable is different from a Singleton because if you write a Singleton class in some module that I want to use, I have to fork your module if I want to make it possible to use two of them. If you use a global variable, I can always make a second global variable.
- Tichy 19y agoOK, I am not even sure anymore what the singleton pattern is _(it's been a while). If it is the class constructor that makes sure you can only ever create one instance, it might be bad. I thought any mechanism for ensuring singleness would qualify for the pattern. Then you examples wouldn't invalidate the connection pool, you could just have to different pools, one for each database. I agree that with the "class instantiation implementation" you could probably not do that.
- bayareaguy 19y agohttp://c2.com/cgi-bin/wiki?SingletonPattern http://c2.com/cgi-bin/wiki?SingletonPattern has some good discussion on it.
- danw 19y agoA difference between Singleton and a global variable is that a singleton can have lazy instantiation