9

Based on an answer I got here, I started to give commons-pool a serious look. My last experience of using it was around 2003, probably version 1.1 or 1.2. Its main user, DBCP, is considered by many as flawed and to be avoided.

Does anyone uses commons pool in production to write pool of your own? What is the best pool type to use? I plan to store client TCP sockets in it.

Is there another generic pool that replaces it?

Community
  • 1
  • 1
David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125

5 Answers5

9

Does anyone uses commons pool in production to write pool of your own?

Yes, I do and the pool holds TCP connections, like you intend it to. It's wired up via Spring, so assuming you understand Spring configuration:

<bean class="com.company.ConnectionSupplier">
<constructor-arg>
  <!-- The ConnectionSupplier wraps an object pool -->
  <bean class="org.apache.commons.pool.impl.GenericObjectPool">
    <constructor-arg>
       <!-- The ObjectPool uses a ConnectionFactory to build new connections -->
       <bean class="com.company.ConnectionFactory">
         <constructor-arg value="server" />
         <constructor-arg value="3000" />  
       </bean>  
    </constructor-arg>
    <property name="maxActive" value="20" />
    <property name="testOnBorrow" value="true" />
  </bean>
</constructor-arg>
</bean>  

The ConnectionFactory extends BasePoolableObjectFactory and is a small wrapper around a SocketFactory.

@First comment: The ConnectionFactory constructor takes a server and a port. In the overriden makeObject(), it creates sockets that connect to that server and port. It returns 'Connection' objects that wrap the created socket with some convenience methods for communicating through the socket.

The connection is tested using a sort of 'ping' or 'echo' provided by the protocol used to communicate over the socket. Should that not have been available, validation/testing of the connection is not really possible, except for asking the socket whether it has been closed. In that case, a Connection in the pool would have been invalidated if it threw an exception and every method using Connections should be prepared for that kind of failure and attempt the same operation with another connection.

Confusion
  • 16,256
  • 8
  • 46
  • 71
1

You should check that the instantation costs more or the fetching from the pool. Because the only valid situation to use the pool is the first.

KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • I cannot instantiate new objects, as the objects are client TCP sockets that need to be alive as long as possible. Closing and opening connections is considered to be DOS attack. – David Rabinowitz Jan 01 '10 at 23:14
  • FYI: we are using commons-pool's GenericObjectPool in a different area but it works very well. – KARASZI István Jan 02 '10 at 11:04
1

Have you looked into Netty or Apache MINA? They will both keep track of your TCP connections and should make implementing whatever communications protocol those TCP sockets will use easier as well.

Stefan L
  • 1,529
  • 13
  • 20
0

First don't use commons-pool 1.3, it has some major issues with multi threaded applications.

Second, Java 5 concurency package has decent pool implementations (see sample here)

Eran Medan
  • 44,555
  • 61
  • 184
  • 276
0

Check out MultiThreadedHttpConnectionManager - it's an Apache Commons HttpClient connection pool manager that will probably fit your need right out of the box.

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
Gandalf
  • 9,648
  • 8
  • 53
  • 88