简体   繁体   中英

Design pattern: Object pool of connections

Recently I started researching more information about the object pool design pattern and I ran into questions that I couldn't found them an answer.

the idea of the object pool pattern is to save expensive resources by reusing them. for examle the connection to the db is a very expensive resource. Therefore i would like to add it to the "pool".

I have always been taught to keep the connection open as little as possible. I may get this all wrong, but in my point of view, creating a new connection and keeping it alive for a long period of time (in the pool) its just a waste of resources. The connection may not be created several times, but it will alway be open.

I would glad to have an explanation.

On the one hand creating new connection may be bad due to long creation time

Other extreme which is full reuse may also be problematic since you are holding resources for long time.

Object pool pattern allows you to configure this and (for example) timeout connection after some time. As every performance-oriented solution it requires some tuning to specific case. This pattern is a place where you should keep this configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM