我正在尝试使用复制槽和Postgresql的Stream API在Java中实现更改数据捕获。 我面临异常的连接故障问题。这是我正在使用的简单代码:
Properties dbConfig = new Properties();
dbConfig.put(USER_PROPERTY, "postgres");
dbConfig.put(PASSWORD_PROPERTY, "password123");
dbConfig.put(MIN_SERVER_VERSION_PROPERTY, "9.6");
dbConfig.put(REPLICATION_PROPERTY, "database");
dbConfig.put(PREFER_QUERY_MODE_PROPERTY, "simple");
String url = "jdbc:postgresql://hostname:5432/hot";
Connection conn = DriverManager.getConnection(url, dbConfig);
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());
}
在这里,如果我评论“ System.out.println(data);”, (这只是打印 控制台中的数据), 与数据库的连接在几秒钟内发生故障,请给我 错误类型:
Exception in thread "main" org.postgresql.util.PSQLException: Database connection failed when reading from copy
at org.postgresql.core.v3.QueryExecutorImpl.readFromCopy(QueryExecutorImpl.java:1037)
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.readPending(V3PGReplicationStream.java:78)
at py.com.personal.kafka.PgStreamProducer.pushToKafka(PgStreamProducer.java:40)
at py.com.personal.App.main(App.java:42)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
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:462)
at org.postgresql.core.PGStream.receive(PGStream.java:446)
at org.postgresql.core.v3.QueryExecutorImpl.processCopyResults(QueryExecutorImpl.java:1170)
at org.postgresql.core.v3.QueryExecutorImpl.readFromCopy(QueryExecutorImpl.java:1035)
... 6 more
我正在研究某些配置,但更改了一些类似的配置,但没有结果:
JVM网络设置 您可以传递这两个强大的超时网络属性,这些属性可以全局适用于所有使用java.net.URLConnection的协议处理程序:
conn.setNetworkTimeout(Executors.newSingleThreadExecutor(), 60000);
PGProperty.CONNECT_TIMEOUT.set(props, 0);
PGProperty.SOCKET_TIMEOUT.set(props, 0);
PGProperty.CANCEL_SIGNAL_TIMEOUT.set(props, 3600);