简体   繁体   English

哪种设计模式可以帮助我实现连接池

[英]which design pattern helps me to implement connection pool

i am trying to create a connection pool for factory class ,which design pattern helps me to create connection pool .user want to specify no of connections and maximum connections externally.I have search for some time, some people proposed to use Singleton or put the initialization code inside some static block. 我正在尝试为工厂类创建一个连接池,这种设计模式可以帮助我创建连接池。用户希望从外部指定连接数和最大连接数。我已经搜索了一段时间,有人建议使用Singleton或放置一些静态块中的初始化代码。 But others said singleton is bad. 但是其他人说单身是坏的。 So, what should be a right design pattern to use for connection pooling? 那么,用于连接池的正确设计模式应该是什么? Thanks. 谢谢。

Singleton is bad for a few reasons eg usually you cannot inject a Mock object. 由于某些原因,Singleton不好,例如通常不能注入Mock对象。 If you provide a mechanism (or do not need) to mock test it, then I do not see a problem. 如果您提供了一种(或不需要)模拟测试的机制,那么我看不到任何问题。

Please note that there are a lot of excellent connection pools available, I would first suggest to look into existing ones. 请注意,这里有很多出色的连接池,我首先建议研究一下现有的连接池。

Database Connection Pooling 数据库连接池

Initially in my application i use apache-commons-dpcp and i faced lot of connection issues and latter after long analysis i use bonecp . 最初,在我的应用程序中,我使用apache-commons-dpcp ,遇到很多连接问题,经过长时间的分析,我使用bonecp I would suggest bonecp connection polling. 我建议bonecp连接轮询。 And Strictly don't use Singleton patten. 并且严格不要使用Singleton Patten。 It will bring you big pain point once your application is go live. 一旦您的应用程序上线,它将给您带来极大的痛苦。

TCP Connection Pooling TCP连接池

For TCP Pooling I suggest to use Netty and Take a look on Apache MINA also. 对于TCP池,我建议使用Netty并同时查看Apache MINA

As previous commenters suggest, use libs like Apache DBCP , C3P0 , BoneCP or HikariCP . 正如以前的评论者所建议的那样,请使用Apache DBCPC3P0BoneCPHikariCP之类的库

But if you want to "invent your own connection pool" and understand how it could be done for educational purpose, start with singletone and extend it to hold preconfigured number of your instances. 但是,如果您想“发明自己的连接池”并了解如何将其用于教学目的,请从单调开始,并将其扩展为可容纳预先配置的实例数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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