Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't have an environment to test this in, at the moment, however, I <em>believe</em> that you should be closing your Connection, Statement, and ResultSet after each query; if any of these leak, it could leave the Connection hanging in an idle (but not necessarily returned to the pool) state.</p> <p>The Connection object you receive should actually be a sort of proxy from the pooling layer; calling <code>close</code> on it releases your "reservation" on that connection and returns it to the pool. (It will not necessarily close the underlying, actual database connection.)</p> <p>Because it could be remaining open (usually will be), unclosed Statements or ResultSets could be interpreted by the pool layer as an indication of being still “busy.”</p> <p>You may be able to inspect (e.g. debugger makes this easy) the Connection object to identify its state at run-time, to confirm this.</p> <p>For simplicity (…) we used the following nasty little routine in the <code>finally</code> blocks after every database connection call: <code>… finally { closeAll (rs, st, con); }</code>, ensuring that they would fall out of context immediately.</p> <pre><code> /** * Close a bunch of things carefully, ignoring exceptions. The * “things” supported, thus far, are: * &lt;ul&gt; * &lt;li&gt;JDBC ResultSet&lt;/li&gt; * &lt;li&gt;JDBC Statement&lt;/li&gt; * &lt;li&gt;JDBC Connection&lt;/li&gt; * &lt;li&gt;Lock:s&lt;/li&gt; * &lt;/ul&gt; * &lt;p&gt; * This is mostly meant for “finally” clauses. * * @param things A set of SQL statements, result sets, and database * connections */ public static void closeAll (final Object... things) { for (final Object thing : things) { if (null != thing) { try { if (thing instanceof ResultSet) { try { ((ResultSet) thing).close (); } catch (final SQLException e) { /* No Op */ } } if (thing instanceof Statement) { try { ((Statement) thing).close (); } catch (final SQLException e) { /* No Op */ } } if (thing instanceof Connection) { try { ((Connection) thing).close (); } catch (final SQLException e) { /* No Op */ } } if (thing instanceof Lock) { try { ((Lock) thing).unlock (); } catch (final IllegalMonitorStateException e) { /* No Op */ } } } catch (final RuntimeException e) { /* No Op */ } } } } </code></pre> <p>This was just syntactic sugar to ensure that nobody forgot to put in the longer, uglier stanza of <code>if (null != con) { try { con.close () } catch (SQLException e) {} }</code> (usually repeated three times for ResultSet, Statement, and Connection); and removed the "visual noise" of what our formatter would turn into a full screen of incidental cleanup code on every block of code that touched the database.</p> <p>(The <code>Lock</code> support in there was for some related, but nasty, deadlock states on potential exceptions, that didn't have much to do with the database at all, but we used in a similar way to reduce the line noise in some thread-synchronization code. This is from an MMO server that might have 4,000 active threads at a time trying to manipulate game objects and SQL tables.)</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