Search code examples
javapostgresqleventspg-jdbc

PGJDBC: could not receive data from client:


Dear StackOverFlowers,

I was trying event-driven LISTENER/NOTIFY on Postgres 9.6 (Windows 10).

I followed PGJDBC example given by Steve Taylor at https://www.openmakesoftware.com/postgresql-listen-notify-events-example/.

I started by downloading pgjdbc-ng-0.7-complete.jar and have put that in my CLASSPATH replacing standard JDBC driver.

When I am trying to connect to Postgres database using pgjdbc driver, I am getting an error:

connection received: host=127.0.0.1 port=50325

connection authorized: user=postgres database=scott

could not receive data from client: An existing connection was forcibly closed by the remote host.

Here are my system variables:

DBHost: localhost

DBName: scott

DBPort: 5432

DBUserName: postgres

DBPassword: postgres

I am not getting past the first hurdle, rest looks like Mount Everest. Please help me. Should you be needing the code, I am following Steve's code ditto.

Further to Joseph Larson's answer, the database is always running. I have connected to Postgres database from PGADMIN and Java successfully. I think issue is with the connect string. From Java when I am using standard JDBC which is provided by Postgres I am using URL like jdbc:postgresql://localhost:5432/dbname but PGJDBC suggests a different connect string like JDBC:PGSQL://localhost:5432/dbname. I tried to connect with that string (forcibly), it did not work. There is no method in PGJDBC PGDataSource for providing URL directly. I had to go through:

dataSource.setHost(DBHost);

dataSource.setPort(5432);

dataSource.setDatabase(DBName);

dataSource.setUser(DBUserName);

dataSource.setPassword(DBPassword);

And what URL it is sending to Database I am not able to figure out. Please suggest me a connect string and this problem is solved.

thanks

Thanks very much for asking me to post error messages:

Exception in thread "main" java.lang.NullPointerException                                                
        at com.impossibl.postgres.system.BasicContext.loadLocale(BasicContext.java:294)                  
        at com.impossibl.postgres.system.BasicContext.init(BasicContext.java:273)                        
        at com.impossibl.postgres.jdbc.PGConnectionImpl.init(PGConnectionImpl.java:251)                  
        at com.impossibl.postgres.jdbc.ConnectionUtil.createConnection(ConnectionUtil.java:182)          
        at com.impossibl.postgres.jdbc.AbstractDataSource.createConnection(AbstractDataSource.java:723)  
        at com.impossibl.postgres.jdbc.PGDataSource.getConnection(PGDataSource.java:66)                  
        at com.impossibl.postgres.jdbc.PGDataSource.getConnection(PGDataSource.java:58)                  
        at PGListenNotify.<init>(PGListenNotify.java:26)                                                 
        at PGListenNotify.main(PGListenNotify.java:37)          

Here is source code:

import java.sql.SQLException;
import java.sql.Statement;

import com.impossibl.postgres.api.jdbc.PGConnection;
import com.impossibl.postgres.jdbc.PGDataSource;

public class PGListenNotify
{
PGConnection connection;

public PGListenNotify()
{
String DBHost = System.getenv("DBHost");
String DBName = System.getenv("DBName");
String DBUserName = System.getenv("DBUserName");
String DBPassword = System.getenv("DBPassword");
try
{
PGDataSource dataSource = new PGDataSource();
dataSource.setHost(DBHost);
dataSource.setPort(5432);
dataSource.setDatabase(DBName);
dataSource.setUser(DBUserName);
dataSource.setPassword(DBPassword);

connection = (PGConnection) dataSource.getConnection();
Statement statement = connection.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
PGListenNotify ln = new PGListenNotify();
}
}                             

Solution

  • This looks like the Windows locale bug in pgjbdc-ng. It has been addressed, try the latest version 0.8.1.

    The latest releases have detailed documentation related to asynchronous notifications here.

    If it still fails to execute on your Windows system, please create an issue here.