在JDBC类中捕获UnknownHostException

时间:2017-08-17 18:07:22

标签: java postgresql jdbc exception-handling c3p0

我正在使用JDBC和c3p0将Swing应用程序连接到PostgreSQL数据库。当连接失效时,它会无法控制地引发几个PSQLException和UnknownHostException,这是预期的。

measureTime

我想立刻捕获那些UnknownHostExceptions,这样它们就不会破坏应用程序的可用性。

我应该在哪里以及如何捕获这些UnknownHostException?我已经尝试过,但没有任何效果。

这是我的BDConnection类:

ago 17, 2017 5:39:25 PM org.postgresql.Driver connect
GRAVE: Connection error: 
org.postgresql.util.PSQLException: El intento de conexión falló.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:275)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
    at org.postgresql.Driver.makeConnection(Driver.java:450)
    at org.postgresql.Driver.connect(Driver.java:252)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
Caused by: java.net.UnknownHostException: academico.ulasalle.edu.pe
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:68)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    ... 13 more

1 个答案:

答案 0 :(得分:1)

立即澄清:UnknownHostException不是直接抛出,而是在某些SQLException中包裹(可能,间接)。

我想你会得到

捕获的包装异常
} catch (final SQLException e) {
    e.printStackTrace();
} finally {

RS_Query()方法中。

一种策略是捕获异常包装UnknownHostException,将其包装在自定义运行时异常中并允许它飞到UI,您可以捕获它并显示错误窗口(或非 - 阻止弹出窗口)

} catch (final SQLException e) {
    if (causedByUnknownHostException(e)) {
        throw new RuntimeUnknownHostException(e);
    } else {
        // processing somehow an exception which is not caused by UnknownHostException
        e.printStackTrace();
    }
} finally {

其中RuntimeUnknownHostException延伸RuntimeException

然后在全局UI异常处理程序中处理它:

try {
    // some code calling conn.RS_Query(...);
} catch (RuntimeUnknownHostException e) {
    ... display the error message
}

检测码:

private boolean causedByUnknownHostException(Throwable t) {
    if (t == null) {
        return false;
    }
    if (t instanceof UnknownHostException) {
        return true;
    }
    return causedByUnknownHostException(t.getCause());
}

为什么取消选中异常(扩展RuntimeException)?这样可以避免在所有中间方法中声明它的负担,因此它可以到达集中式UI异常处理程序。