[GENERAL] pgjdbc logical replication client throwing exception - Mailing list pgsql-general
From | Dipesh Dangol |
---|---|
Subject | [GENERAL] pgjdbc logical replication client throwing exception |
Date | |
Msg-id | CA+=-RVb9_AgzBJ5OXu2gMCq8NuJaNvHhePqr9b-nojKitOATgw@mail.gmail.com Whole thread Raw |
Responses |
Re: [GENERAL] [HACKERS] pgjdbc logical replication client throwing exception
|
List | pgsql-general |
hi,
I am trying to implement logical replication stream API of postgresql.I am facing unusual connection breakdown problem. Here is the simple code that I am
using to read WAL file:
String url = "jdbc:postgresql://pcnode2:543 2/benchmarksql";
Properties props = new Properties();
PGProperty.USER.set(props, "benchmarksql");
PGProperty.PASSWORD.set(props, "benchmarksql");
PGProperty.ASSUME_MIN_SERVER_V ERSION.set(props, "9.4");
PGProperty.REPLICATION.set(pro ps, "database");
PGProperty.PREFER_QUERY_MODE.s et(props, "simple");
Connection conn = DriverManager.getConnection(ur l, props);
PGConnection replConnection = conn.unwrap(PGConnection.class );
PGReplicationStream stream = replConnection.getReplicationA PI()
.replicationStream().logical()
.withSlotName("replication_slo t3")
.withSlotOption("include-xids" , true)
.withSlotOption("include-times tamp", "on")
.withSlotOption("skip-empty-xa cts", true)
.withStatusInterval(20, TimeUnit.MILLISECONDS).start() ;
while (true) {
ByteBuffer msg = stream.read();
if (msg == null) {
TimeUnit.MILLISECONDS.sleep(10 L);
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.ge tLastReceiveLSN());
stream.setFlushedLSN(stream.ge tLastReceiveLSN());
}
Properties props = new Properties();
PGProperty.USER.set(props, "benchmarksql");
PGProperty.PASSWORD.set(props, "benchmarksql");
PGProperty.ASSUME_MIN_SERVER_V
PGProperty.REPLICATION.set(pro
PGProperty.PREFER_QUERY_MODE.s
Connection conn = DriverManager.getConnection(ur
PGConnection replConnection = conn.unwrap(PGConnection.class
PGReplicationStream stream = replConnection.getReplicationA
.replicationStream().logical()
.withSlotName("replication_slo
.withSlotOption("include-xids"
.withSlotOption("include-times
.withSlotOption("skip-empty-xa
.withStatusInterval(20, TimeUnit.MILLISECONDS).start()
while (true) {
ByteBuffer msg = stream.read();
if (msg == null) {
TimeUnit.MILLISECONDS.sleep(10
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.ge
stream.setFlushedLSN(stream.ge
}
Even the slightest modification in the code like commenting System.out.println(data);
which is just printing the data in the console, causes connection breakdown problem with
following error msg
org.postgresql.util.PSQLExcept ion: Database connection failed when reading from copy
at org.postgresql.core.v3.QueryEx ecutorImpl.readFromCopy(QueryE xecutorImpl.java:1028)
at org.postgresql.core.v3.CopyDua lImpl.readFromCopy(CopyDualImp l.java:41)
at org.postgresql.core.v3.replica tion.V3PGReplicationStream.rec eiveNextData(V3PGReplicationSt ream.java:155)
at org.postgresql.core.v3.replica tion.V3PGReplicationStream.rea dInternal(V3PGReplicationStrea m.java:124)
at org.postgresql.core.v3.replica tion.V3PGReplicationStream. read(V3PGReplicationStream. java:70)
at Server.main(Server.java:52)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.soc ketRead0(Native Method)
at java.net.SocketInputStream.soc ketRead(SocketInputStream.java :116)
at java.net.SocketInputStream.rea d(SocketInputStream.java:171)
at java.net.SocketInputStream.rea d(SocketInputStream.java:141)
at org.postgresql.core.VisibleBuf feredInputStream.readMore(Visi bleBufferedInputStream.java: 140)
at org.postgresql.core.VisibleBuf feredInputStream.ensureBytes(V isibleBufferedInputStream.java :109)
at org.postgresql.core.VisibleBuf feredInputStream.read(VisibleB ufferedInputStream.java:191)
at org.postgresql.core.PGStream.r eceive(PGStream.java:495)
at org.postgresql.core.PGStream.r eceive(PGStream.java:479)
at org.postgresql.core.v3.QueryEx ecutorImpl.processCopyResults( QueryExecutorImpl.java:1161)
at org.postgresql.core.v3.QueryEx ecutorImpl.readFromCopy(QueryE xecutorImpl.java:1026)
... 5 more
at org.postgresql.core.v3.QueryEx
at org.postgresql.core.v3.CopyDua
at org.postgresql.core.v3.replica
at org.postgresql.core.v3.replica
at org.postgresql.core.v3.replica
at Server.main(Server.java:52)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.soc
at java.net.SocketInputStream.soc
at java.net.SocketInputStream.rea
at java.net.SocketInputStream.rea
at org.postgresql.core.VisibleBuf
at org.postgresql.core.VisibleBuf
at org.postgresql.core.VisibleBuf
at org.postgresql.core.PGStream.r
at org.postgresql.core.PGStream.r
at org.postgresql.core.v3.QueryEx
at org.postgresql.core.v3.QueryEx
... 5 more
But due to this unusual behavior I couldn't implement properly.
Can somebody give me some hint how to solve this problem.
Thank you.
pgsql-general by date: