1

In my DB connection, the stack trace for a failed connection is always printed to the terminal, is it possible to turn this off?

try {
    Class.forName("org.postgresql.Driver");
    this.connection = DriverManager.getConnection("jdbc:postgresql://" + getHost() + ":" + getPort() + "/" + getDatabase(), getUser(), getPassword());
} catch (Exception e) {
    // deal with exception
}

I currently have the local DB turned off so it will always time out and try to catch the exception.

Example output looks like:

    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
Royal Wares
  • 1,192
  • 10
  • 23

1 Answers1

2

This is a problem of pgjdbc, in class org.postgresql.core.v3.ConnectionFactoryImpl it was fixed in this commit.

And this fix was released on version 42.2.3.

So, if you can update your pgjdbc library, application will show logs only if Logger is configured to FINE level.