Skip to content

Commit

Permalink
[postgres] Close jdbc connection after creating replication slot for …
Browse files Browse the repository at this point in the history
…stream split (#2425)
  • Loading branch information
loserwang1024 authored Sep 26, 2023
1 parent a8fb5b0 commit 82e3053
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ public PostgresConnection openJdbcConnection() {
return (PostgresConnection) openJdbcConnection(sourceConfig);
}

public PostgresReplicationConnection openPostgresReplicationConnection() {
public PostgresReplicationConnection openPostgresReplicationConnection(
PostgresConnection jdbcConnection) {
try {
PostgresConnection jdbcConnection =
(PostgresConnection) openJdbcConnection(sourceConfig);
PostgresConnectorConfig pgConnectorConfig = sourceConfig.getDbzConnectorConfig();
TopicSelector<TableId> topicSelector = PostgresTopicSelector.create(pgConnectorConfig);
PostgresConnection.PostgresValueConverterBuilder valueConverterBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import io.debezium.connector.postgresql.connection.PostgresReplicationConnection;
import io.debezium.connector.postgresql.spi.SlotState;

import java.sql.SQLException;

/**
* The Postgres source enumerator that enumerates receive the split request and assign the split to
* source readers.
Expand Down Expand Up @@ -60,32 +58,25 @@ public void start() {
* reading the globalStreamSplit to catch all data changes.
*/
private void createSlotForGlobalStreamSplit() {
SlotState slotInfo = null;
try (PostgresConnection connection = postgresDialect.openJdbcConnection()) {
slotInfo =
SlotState slotInfo =
connection.getReplicationSlotState(
postgresDialect.getSlotName(), postgresDialect.getPluginName());
} catch (SQLException e) {
throw new RuntimeException(
String.format(
"Fail to get the replication slot info, the slot name is %s.",
postgresDialect.getSlotName()),
e);
}

// skip creating the replication slot when the slot exists.
if (slotInfo != null) {
return;
}

try {
// skip creating the replication slot when the slot exists.
if (slotInfo != null) {
return;
}
PostgresReplicationConnection replicationConnection =
postgresDialect.openPostgresReplicationConnection();
postgresDialect.openPostgresReplicationConnection(connection);
replicationConnection.createReplicationSlot();
replicationConnection.close(false);

} catch (Throwable t) {
throw new FlinkRuntimeException(
"Create Slot For Global Stream Split failed due to ", t);
String.format(
"Fail to get or create slot for global stream split, the slot name is %s. Due to: ",
postgresDialect.getSlotName()),
t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
Expand Down Expand Up @@ -155,10 +154,6 @@ public void createAndInitialize() {
}
}

public Connection getJdbcConnection() throws SQLException {
return DriverManager.getConnection(container.getJdbcUrl(), username, password);
}

private String convertSQL(final String sql) {
return sql.replace("$DBNAME$", schemaName);
}
Expand Down

0 comments on commit 82e3053

Please sign in to comment.