Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top