connection breakdown issue in postgresql while using replication slot with the help of stream api java

From: Dipesh Dangol <ddipeshdan(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: connection breakdown issue in postgresql while using replication slot with the help of stream api java
Date: 2017-09-01 12:36:08
Message-ID: CA+=-RVY_6gAghMsN+AyGgxRtb0M=9PaotSYCVykB04ysw68RKA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

hi,

I am trying to implement Change Data Capture in java using replication slot
and Stream API of postgresql.
I am facing unusual connection breakdown problem. Here is the simple code
that I am using:

String url = "jdbc:postgresql://pcnode2:5432/benchmarksql";
Properties props = new Properties();
PGProperty.USER.set(props, "benchmarksql");
PGProperty.PASSWORD.set(props, "benchmarksql");
PGProperty.ASSUME_MIN_SERVER_VERSION.set(props, "9.4");
PGProperty.REPLICATION.set(props, "database");
PGProperty.PREFER_QUERY_MODE.set(props, "simple");

Connection conn = DriverManager.getConnection(url, props);
PGConnection replConnection = conn.unwrap(PGConnection.class);

PGReplicationStream stream = replConnection.getReplicationAPI()
.replicationStream().logical()
.withSlotName("replication_slot3")
.withSlotOption("include-xids", true)
.withSlotOption("include-timestamp", "on")
.withSlotOption("skip-empty-xacts", true)
.withStatusInterval(20, TimeUnit.MILLISECONDS).start();
while (true) {

ByteBuffer msg = stream.read();

if (msg == null) {
TimeUnit.MILLISECONDS.sleep(10L);
continue;
}

int offset = msg.arrayOffset();
byte[] source = msg.array();
int length = source.length - offset;
String data = new String(source, offset, length);
* //System.out.println(data);*

stream.setAppliedLSN(stream.getLastReceiveLSN());
stream.setFlushedLSN(stream.getLastReceiveLSN());

}

Here, if I comment "*System.out.println(data)*;" (which is just printing
the data in the console),
connection to the database breaks down within few second and give me this
kind of error:

org.postgresql.util.PSQLException: Database connection failed when reading
from copy
at
org.postgresql.core.v3.QueryExecutorImpl.readFromCopy(QueryExecutorImpl.java:1028)
at
org.postgresql.core.v3.CopyDualImpl.readFromCopy(CopyDualImpl.java:41)
at
org.postgresql.core.v3.replication.V3PGReplicationStream.receiveNextData(V3PGReplicationStream.java:155)
at
org.postgresql.core.v3.replication.V3PGReplicationStream.readInternal(V3PGReplicationStream.java:124)
at
org.postgresql.core.v3.replication.V3PGReplicationStream.read(V3PGReplicationStream.java:70)
at Server.main(Server.java:52)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at
org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)
at
org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)
at
org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:191)
at org.postgresql.core.PGStream.receive(PGStream.java:495)
at org.postgresql.core.PGStream.receive(PGStream.java:479)
at
org.postgresql.core.v3.QueryExecutorImpl.processCopyResults(QueryExecutorImpl.java:1161)
at
org.postgresql.core.v3.QueryExecutorImpl.readFromCopy(QueryExecutorImpl.java:1026)
... 5 more

if I uncomment that line *"System.out.println(data)" ,* everything works
fine. Is this
a valid behavior?

Best regards,
Dipesh Dangol

Browse pgsql-novice by date

  From Date Subject
Next Message Alexey Luchko 2017-09-03 17:09:15 jsonb and null
Previous Message Yann Salaün 2017-08-25 14:27:44 Re: Does pg store all `timestamp with time zone` in localtime? Why?