tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: configure JNDI to avoid error "Last packet sent to the server was xxxxx ms ago"
Date Sun, 07 Dec 2008 14:35:37 GMT
Hash: SHA1

Bob (why not?), wrote:
> The second message -- connection has already been closed -- most likely arises from my
code structure, which is:
> Connection connection = null;
> PreparedStatement prepStmt = null;
> try
> {
>    Connection connection = getConnection();
>    PreparedStatement prepStmt = connection.prepareStatement(...);
>    ...
> }
> finally
> {
>    if (prepStmt != null) prepStmt.close();
>    if (connection != null) connection.close();
> }

You should use try/catch blocks around the calls to your close methods.
Otherwise, exceptions thrown in your main try/catch block might be
masked by close errors. You want to make sure the original exception
gets thrown, not a secondary one.

> The call to getConnection() in the try block gets a connection from
> the pool.  But the server has closed this connection.  Therefore the
> call to prepareStatement throws a SQLException which is the last
> packet exception.

This is what validationQuery is supposed to fix. When properly
configured, you don't have to worry about this.

> But it's just too much code to write!

So don't write it that way. It's not even remotely necessary. The
finally block will still run "during" an exception throw. That's the
point of the finally clause. Just let the original exception be thrown
by the runtime by catching and logging the secondary one.

> I removed minEvictableIdleTimeMillis and testWhileIdle.

I think that's a good idea.

> Is there any doc about the ping in "/*ping*/ select 1"?

Search for "ping" in the Connector/J documentation. I could only find it
in the changelog, unfortunately.

> And yes, I got these errors even without my driver.

That's good to know.

- -chris
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message