在我的数据库连接中,失败连接的堆栈跟踪总是打印到终端上,是否可以将其关闭?
try {
Class.forName("org.postgresql.Driver");
this.connection = DriverManager.getConnection("jdbc:postgresql://" + getHost() + ":" + getPort() + "/" + getDatabase(), getUser(), getPassword());
} catch (Exception e) {
// deal with exception
}
我当前关闭了本地数据库,因此它将始终超时并尝试捕获异常。
示例输出如下:
Sep 21, 2018 2:07:56 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:452)
at org.postgresql.Driver.connect(Driver.java:254)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at database.psql.PsqlDatabase.connect(PsqlDatabase.java:41)
at database.DatabaseFactory.getDatabase(DatabaseFactory.java:15)
at api.acme.AcmeApi.queuePlacementNotifications(AcmeApi.java:101)
at poller.AcmePoller.run(AcmePoller.java:28)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.postgresql.core.PGStream.<init>(PGStream.java:69)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:156)
... 11 more
答案 0 :(得分:2)
这是pgjdbc的问题,在类 org.postgresql.core.v3.ConnectionFactoryImpl 中已在this commit中进行了修复。
此修复程序已在版本42.2.3上发布。
因此,如果您可以更新pgjdbc库,则仅当Logger配置为FINE级别时,应用程序才会显示日志。