Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would not return the "real" connection object from the pool, but a wrapper which gives the <em>pool</em> control of connection life cycle, instead of the client. </p> <p>Assume you have a really simple connection, which you can read <code>int</code> values from:</p> <pre><code>interface Connection { int read(); // reads an int from the connection void close(); // closes the connection } </code></pre> <p>An implementation reading from a stream could look like this (ignoring exceptions, EOF handling, etc):</p> <pre><code>class StreamConnection implements Connection { private final InputStream input; int read(){ return input.read(); } void close(){ input.close(); } } </code></pre> <p>Furthermore, let's assume you have a pool for <code>StreamConnection</code>s that looks like this (again, ignoring exceptions, concurrency, etc):</p> <pre><code>class StreamConnectionPool { List&lt;StreamConnection&gt; freeConnections = openSomeConnectionsSomehow(); StreamConnection borrowConnection(){ if (freeConnections.isEmpty()) throw new IllegalStateException("No free connections"); return freeConnections.remove(0); } void returnConnection(StreamConnection conn){ freeConnections.add(conn); } } </code></pre> <p>The basic idea here is OK, but we can't be sure the connections are returned, and we can't be sure they aren't closed and then returned, or that you don't return a connection which came from another source altogether.</p> <p>The solution is (of course) another layer of indirection: Make a pool which returns a wrapper <code>Connection</code> which, instead of closing the underlying connection when <code>close()</code> is called, returns it to the pool:</p> <pre><code>class ConnectionPool { private final StreamConnectionPool streamPool = ...; Connection getConnection() { final StreamConnection realConnection = streamPool.borrowConnection(); return new Connection(){ private boolean closed = false; int read () { if (closed) throw new IllegalStateException("Connection closed"); return realConnection.read(); } void close() { if (!closed) { closed = true; streamPool.returnConnection(realConnection); } } protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } } }; } } </code></pre> <p>This <code>ConnectionPool</code> would be the only thing the client code ever sees. Assuming it is the sole owner of the <code>StreamConnectionPool</code>, this approach has several advantages:</p> <p><strong>Reduced complexity and minimal impact on client code</strong> - the only difference between opening connections yourself and using the pool is that you use a factory to get hold of <code>Connection</code>s (which you might already do, if you're using dependency injection). Most importantly, you always clean up your resources in the same way, i.e., by calling <code>close()</code>. Just like you don't care what <code>read</code> does, as long as it gives you the data you need, you don't care what <code>close()</code> does, as long as it releases the resources you've claimed. You shouldn't have to think whether this connection is from a pool or not.</p> <p><strong>Protection against malicious/incorrect usage</strong> - clients can only return resources they've retrieved from the pool; they can't close the underlying connections; they can't use connections they've already returned... etc.</p> <p><strong>"Guaranteed" returning of resources</strong> - thanks to our <code>finalize</code> implementation, even if all references to a borrowed <code>Connection</code> is lost, it is still returned to the pool (or does at least stand a chance to be returned). The connection will of course be held longer than necessary this way - possibly indefinitely, since finalization isn't guaranteed to ever run - but it's a small improvement.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload