Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postgres] close jdbc connection when close replication connection #2425

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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