开发者

C++ MySQL and multithreading - 1 DB connection per user?

开发者 https://www.devze.com 2023-01-06 13:27 出处:网络
Is in multithreaded application suitable to have 1 connection per 1 con开发者_开发技巧nected client? To me it seems ineffective but if there is not connection pooling, how it could be done when one wa

Is in multithreaded application suitable to have 1 connection per 1 con开发者_开发技巧nected client? To me it seems ineffective but if there is not connection pooling, how it could be done when one wants to let each connection communicate with DB? Thanks


If you decide to share a connection amongst threads, you need to be sure that one thread completely finishes with the connection before another uses it (use a mutex, semaphore, or critical section to protect the connections). Alternately, you could write your own connection pool. This is not as hard as it sounds ... make 10 connections (or however big your pool needs to be) on startup and allocate/deallocate them on demand. Again protecting with mutex/cs/sema.


That depends on your architecture.

It sounds like you're using a server->distributed client model? In that case I would implement some sort of a layer for DB access, and hide connection pooling, etc. behind a data access facade.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号