I've a multi-user supporting server
which is based on qxmpp
library written in Qt
and exdended by inheriting QXmppServerExtension
. So, my question is: Considering multi-user support in my server, whether it's safe or not to use a Singletone class for database connection and holding a connection for QSqlQuery?
Since, all the extensions that work via database use it respectively.

- 51,870
- 39
- 111
- 135

- 665
- 9
- 33
-
Why would you prefer singleton over multiton? – László Papp Jan 01 '14 at 07:30
-
Cause, I would like to save memory in server side. – elgolondrino Jan 01 '14 at 07:49
-
1Do you have concrete benchmarks how much you would save with it? – László Papp Jan 01 '14 at 07:50
-
What do you advice to use multiton? – elgolondrino Jan 01 '14 at 08:04
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44265/discussion-between-elgolondrino-and-laszlo-papp) – elgolondrino Jan 01 '14 at 09:08
-
Well, as far as I know QXmppServer holds a list of connected clients instead of Threads, so from that point I've decided to use Singletone pattern. By the way, to tell the truth I'm not aware of Q_GLOBAL_STATIC? Is it concerning the certain pattern? – elgolondrino Jan 01 '14 at 09:51
-
To save memory that is it! – elgolondrino Jan 01 '14 at 10:20
-
Okay, per a client connected it takes 216KB memory. What do you think is it a lot for a server? – elgolondrino Jan 01 '14 at 10:37
-
How many clients do you have, and how much memory on the server, what other services will be running, etc? It depends a lot on more context. :) – László Papp Jan 01 '14 at 10:46
-
More or less, 1000 clients and MySQL service will be running. 16 GBytes of RAM. – elgolondrino Jan 01 '14 at 11:05
-
1That is about 220 MB for 16 GB ram. Do you agree that it is negligible unless you have other operations heavily using the memory, but in that case, this might not be the best place to look for optimization? – László Papp Jan 01 '14 at 12:20
1 Answers
You can read more about singletons in general why they are considered bad in many cases:
What is so bad about Singletons?
I would personally avoid them in your without the context more. The only reason you bought up so far for singletons rather than passing references or that kind around, you would spare some memory for the server service.
Based on the discussion, it is always better to make sure you have some benchmark in place when the complexity difference is not obvious. In your special case, with additional 216K memory for about 1000 clients, you would get about 250 MB additional memory usage.
I am rounding this number up a bit just in case. This is in no way comparable to the capacity of your server which seems to be about 16 GB. If you have other memory hungry services, those would probably be a more offending place to look into optimization.
That being said, if you wish to go for singletons, you could implement that on top of the Q_GLOBAL_STATIC or Q_GLOBAL_STATIC_WITH_ARGS macros in Qt. Note that this was also public in Qt 4, although undocumented. As far as I know, it did not have proper thread-safety in Qt 4 though which was significantly improved in Qt 5.

- 1
- 1

- 51,870
- 39
- 111
- 135