Bernie.T
Bernie.T

Reputation: 11

Postgresql connecting issue with pgjdbc-ng

I am trying to connect postgresql with pgjdbc-ng(0.8.9) for asynchronous listener. Everything work fine when developing, but I got connect timeout in SIT environment.
There is a postgresql 13 server in SIT, force client connect with ssl, use tls 1.3 only.
Here is the code:

@Bean
public PGConnection getPGConnection() throws SQLException {
    var connectionString = String
        .format("jdbc:pgsql://%s/%s?ssl.mode=require", host, database);
    return DriverManager
            .getConnection(connectionString, user, password)
            .unwrap(PGConnection.class);
}

And here is the stacktrace print out:

Cause by: java.io.IOException: Timeout starting connection
    at com.impossibl.postgres.protocol.v30.ServerConnectionFactory.startup
    ...

It confuses me because only the connection from above code got timeout error. Other connections of Spring Data JAP(PostgreSQL JDBC Driver) are totally correct in same application at same time. And the worst is I can't reproduce in my enviroment.
At first I thought it was about tls protocol verison because the application need to work on a mechine which openssl version in 1.0.1(tls 1.3 is not supported). But it seens not because other connections work properly.
I kown it was not rejected from connection limit for postgres user because it throw different exception. And I've tried use different way to get connection like:

var dataSource = new PGDataSource();
dataSource.setUrl("jdbc:pgsql://host/dbname");
dataSource.setUser(user);
dataSource.setPassword(password);  
dataSource.setSslMode(SSLMode.Require.name());
var conn = dataSource.getConnection().unwrap(PGConnection.class);

It turned into same result, work in developing and fail in SIT environment.


Dose anybody has similar experiences?
Or please tell me what should I check with DBA, pg_hba.conf or something else?
Thank you.

Upvotes: 0

Views: 1462

Answers (1)

Sachanski
Sachanski

Reputation: 91

I was in (what I think is) a similar situation.

I have one HikariDataSource for connections to the database which works fine both locally and on the dev environment and one PGDataSource (from pgjdbc-ng) which works locally but timeouts on the dev environment (I am also using it for NOTIFY/LISTEN).

Turns out the problem was due to insufficient numbers. Specifically the remote environment is a k8s cluster which starts docker containers. Apparently there is not enough entropy in docker containers so a call to SecureRandom.getInstanceStrong().nextInt() blocks since it uses /dev/random, because this is the default algorithm chosen by Java.

The proper fix would be to configure the docker containers to provide enough entropy for /dev/random not to hang, however right now I am using a workaround by setting Security.setProperty("securerandom.strongAlgorithms", "NativePRNG") at startup which makes Java choose a non-blocking algorithm for SecureRandom.getInstanceStrong().

Upvotes: 1

Related Questions