Skip to content

Commit 5607d8b

Browse files
Fixes #1141 do not throw SQLException when calling isClosed() or close() on a already closed Connection, as per JDBC specification.
1 parent 8921bb0 commit 5607d8b

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

src/main/java/com/zaxxer/hikari/pool/ProxyConnection.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,18 @@
1616

1717
package com.zaxxer.hikari.pool;
1818

19-
import static com.zaxxer.hikari.util.ClockSource.currentTime;
19+
import com.zaxxer.hikari.util.FastList;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
2022

2123
import java.lang.reflect.InvocationHandler;
2224
import java.lang.reflect.Proxy;
23-
import java.sql.CallableStatement;
24-
import java.sql.Connection;
25-
import java.sql.DatabaseMetaData;
26-
import java.sql.PreparedStatement;
27-
import java.sql.SQLException;
28-
import java.sql.Savepoint;
29-
import java.sql.Statement;
30-
import java.sql.Wrapper;
25+
import java.sql.*;
3126
import java.util.HashSet;
3227
import java.util.Set;
3328
import java.util.concurrent.Executor;
3429

35-
import org.slf4j.Logger;
36-
import org.slf4j.LoggerFactory;
37-
38-
import com.zaxxer.hikari.util.FastList;
30+
import static com.zaxxer.hikari.util.ClockSource.currentTime;
3931

4032
/**
4133
* This is the proxy class for java.sql.Connection.
@@ -264,6 +256,7 @@ public final void close() throws SQLException
264256

265257
/** {@inheritDoc} */
266258
@Override
259+
@SuppressWarnings("RedundantThrows")
267260
public boolean isClosed() throws SQLException
268261
{
269262
return (delegate == ClosedConnection.CLOSED_CONNECTION);
@@ -447,7 +440,7 @@ public void setSchema(String schema) throws SQLException
447440
@Override
448441
public final boolean isWrapperFor(Class<?> iface) throws SQLException
449442
{
450-
return iface.isInstance(delegate) || (delegate instanceof Wrapper && delegate.isWrapperFor(iface));
443+
return iface.isInstance(delegate) || (delegate != null && delegate.isWrapperFor(iface));
451444
}
452445

453446
/** {@inheritDoc} */
@@ -458,7 +451,7 @@ public final <T> T unwrap(Class<T> iface) throws SQLException
458451
if (iface.isInstance(delegate)) {
459452
return (T) delegate;
460453
}
461-
else if (delegate instanceof Wrapper) {
454+
else if (delegate != null) {
462455
return delegate.unwrap(iface);
463456
}
464457

@@ -477,12 +470,18 @@ private static Connection getClosedConnection()
477470
{
478471
InvocationHandler handler = (proxy, method, args) -> {
479472
final String methodName = method.getName();
480-
if ("abort".equals(methodName)) {
481-
return Void.TYPE;
473+
if ("isClosed".equals(methodName)) {
474+
return Boolean.TRUE;
482475
}
483476
else if ("isValid".equals(methodName)) {
484477
return Boolean.FALSE;
485478
}
479+
if ("abort".equals(methodName)) {
480+
return Void.TYPE;
481+
}
482+
if ("close".equals(methodName)) {
483+
return Void.TYPE;
484+
}
486485
else if ("toString".equals(methodName)) {
487486
return ClosedConnection.class.getCanonicalName();
488487
}

0 commit comments

Comments
 (0)