I have implemented JDBCConnectionPooling using Object Pool Design Pattern for database connections. I am just wondering for real world examples for Object Pool Design Pattern. Can anyone have an idea about a real-world example or from any Java Libraries for Object Pool Design Pattern?
-
Every app that uses database connections for multiple simultaneous requests will have a pooled database connection. Every app server has one. Is this so hard to find? What other shared resources (e.g. queue connections, task executors, HTTP service clients) can you think of? – duffymo May 04 '18 at 11:31
-
2You really should not implement your own JDBC connection pool. There are libraries out there that are battle-tested; don't reinvent the wheel. – Mark Rotteveel May 04 '18 at 11:46
2 Answers
Take a look at java.lang.String it uses a cache of strings, and if inline there is something like
String foo = "foobar";
String bar = "foobar";
then it will be translated to something like:
class StringPool {
public static String static_foo = "foobar";
}
String foo = StringPool.foo;
String bar = StringPool.foo;
Note, this is a very simplified example of the String pooling used by java.
For more reading I suggest you read What is the Java string pool and how is "s" different from new String("s")?

- 27,901
- 14
- 88
- 133
The Integer
class caches values as explained in Integer.valueOf
public static Integer valueOf(int i)
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
The range -128 to 127 is not really a pool since this will be a static set of values but it is design to also be able to cache other values if needed. Note that I never noticed that behavior outside of the defined ranged. So :
Integer.valueOf(5) == Integer.valueOf(5) //true
Integer.valueOf(1234) == Integer.valueOf(1234) //false even if it could be true based on the javadoc