问题
What is the good design pattern for implementing one connection (or generally resource) pool? For example, one tomcat server connects to one mysql server, and all the requests share a mysql connection pool on tomcat server. I have search for some time, some people proposed to use Singleton or put the initialization code inside some static block. But others said singleton is bad. So, what should be a right design pattern to use for connection pooling? Thanks.
回答1:
Object (Resource) Pool is a Design Pattern.
There is not a single Connection pool in Tomcat, instead it allows you to define multiple pools. You then control the scope and access to each pool via a service provider as either the global server, a service, an engine, a host or just a single context. The application looks up connection pool and the service provider ensures the defined rules are upheld.
However, your question it so open ended we cannot feasible suggest the most appropriate solution for you.
回答2:
FlyWeight Design pattern used for Connection Pooling . as stated by GOF "Facilitates the reuse of many fine grained objects, making the utilization of large numbers of objects more efficient."
回答3:
I prefer singleton design pattern.
It will create Instance single time and Used frequently.
for timely connection pooling Singleton
it will easy and good.
来源:https://stackoverflow.com/questions/9256758/what-is-the-good-design-pattern-for-connection-pooling