Skip to content
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
3 changes: 2 additions & 1 deletion jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.core.grpc.GrpcTransportBuilder;
import tech.ydb.core.impl.SingleChannelTransport;
import tech.ydb.core.settings.BaseRequestSettings;
import tech.ydb.jdbc.YdbPrepareMode;
import tech.ydb.jdbc.YdbTracer;
Expand Down Expand Up @@ -238,7 +239,7 @@ public static YdbContext createContext(YdbConfig config) throws SQLException {
});
});

grpcTransport = builder.build();
grpcTransport = config.isUseDiscovery() ? builder.build() : new SingleChannelTransport(builder);

PooledTableClient.Builder tableClient = PooledTableClient.newClient(
GrpcTableRpc.useTransport(grpcTransport)
Expand Down
10 changes: 10 additions & 0 deletions jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class YdbConfig {
static final YdbProperty<String> USE_PREFIX_PATH = YdbProperty.string("usePrefixPath",
"Add prefix path to all operations performed by driver");

static final YdbProperty<Boolean> USE_DISCOVERY = YdbProperty.bool("useDiscovery",
"Use discovery (client balancing) for YDB cluster connection", true);

static final YdbProperty<Boolean> FULLSCAN_DETECTOR_ENABLED = YdbProperty.bool(
"jdbcFullScanDetector", "Enable analizator for collecting query stats", false
);
Expand All @@ -64,6 +67,7 @@ public class YdbConfig {
private final int preparedStatementsCacheSize;

private final boolean useQueryService;
private final boolean useDiscovery;
private final YdbValue<String> usePrefixPath;

private final boolean fullScanDetectorEnabled;
Expand All @@ -83,6 +87,7 @@ private YdbConfig(
this.preparedStatementsCacheSize = Math.max(0, PREPARED_STATEMENT_CACHE_SIZE.readValue(props).getValue());

this.useQueryService = USE_QUERY_SERVICE.readValue(props).getValue();
this.useDiscovery = USE_DISCOVERY.readValue(props).getValue();
this.usePrefixPath = USE_PREFIX_PATH.readValue(props);

this.fullScanDetectorEnabled = FULLSCAN_DETECTOR_ENABLED.readValue(props).getValue();
Expand Down Expand Up @@ -124,6 +129,10 @@ public boolean isUseQueryService() {
return this.useQueryService;
}

public boolean isUseDiscovery() {
return this.useDiscovery;
}

public boolean hasPrefixPath() {
return usePrefixPath.hasValue();
}
Expand Down Expand Up @@ -188,6 +197,7 @@ public DriverPropertyInfo[] toPropertyInfo() throws SQLException {
YdbConfig.CACHE_CONNECTIONS_IN_DRIVER.toInfo(properties),
YdbConfig.PREPARED_STATEMENT_CACHE_SIZE.toInfo(properties),
YdbConfig.USE_QUERY_SERVICE.toInfo(properties),
YdbConfig.USE_DISCOVERY.toInfo(properties),
YdbConfig.USE_PREFIX_PATH.toInfo(properties),

YdbConnectionProperties.LOCAL_DATACENTER.toInfo(properties),
Expand Down
84 changes: 63 additions & 21 deletions jdbc/src/test/java/tech/ydb/jdbc/YdbDriverExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import java.sql.Types;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import tech.ydb.test.junit5.YdbHelperExtension;

Expand All @@ -21,23 +22,10 @@ public class YdbDriverExampleTest {
@RegisterExtension
private static final YdbHelperExtension ydb = new YdbHelperExtension();

private static String jdbcURL() {
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
.append(ydb.useTls() ? "grpcs://" : "grpc://")
.append(ydb.endpoint())
.append("/?database=")
.append(ydb.database());

if (ydb.authToken() != null) {
jdbc.append("&").append("token=").append(ydb.authToken());
}

return jdbc.toString();
}

@Test
public void testYdb() throws SQLException {
try (Connection connection = DriverManager.getConnection(jdbcURL())) {
@ParameterizedTest
@EnumSource(Mode.class)
public void testYdb(Mode mode) throws SQLException {
try (Connection connection = DriverManager.getConnection(mode.getJdbcURL())) {
try {
connection.createStatement()
.execute("drop table table_sample");
Expand Down Expand Up @@ -148,9 +136,10 @@ public void testYdb() throws SQLException {
}
}

@Test
public void testYdbNotNull() throws SQLException {
try (Connection connection = DriverManager.getConnection(jdbcURL())) {
@ParameterizedTest
@EnumSource(Mode.class)
public void testYdbNotNull(Mode mode) throws SQLException {
try (Connection connection = DriverManager.getConnection(mode.getJdbcURL())) {
try {
connection.createStatement().execute("drop table table_sample");
} catch (SQLException e) {
Expand Down Expand Up @@ -249,4 +238,57 @@ public void testYdbNotNull() throws SQLException {
}
}
}

private enum Mode {
BASE {
@Override
String getJdbcURL() {
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
.append(ydb.useTls() ? "grpcs://" : "grpc://")
.append(ydb.endpoint())
.append("/")
.append(ydb.database());

if (ydb.authToken() != null) {
jdbc.append("?").append("token=").append(ydb.authToken());
}

return jdbc.toString();
}
},
OLD_STYLE {
@Override
String getJdbcURL() {
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
.append(ydb.useTls() ? "grpcs://" : "grpc://")
.append(ydb.endpoint())
.append("/?database=")
.append(ydb.database());

if (ydb.authToken() != null) {
jdbc.append("&").append("token=").append(ydb.authToken());
}

return jdbc.toString();
}
},
NO_DISCOVERY {
@Override
String getJdbcURL() {
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
.append(ydb.useTls() ? "grpcs://" : "grpc://")
.append(ydb.endpoint())
.append(ydb.database())
.append("?useDiscovery=false");

if (ydb.authToken() != null) {
jdbc.append("&").append("token=").append(ydb.authToken());
}

return jdbc.toString();
}
};

abstract String getJdbcURL();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ static DriverPropertyInfo[] defaultPropertyInfo(@Nullable String localDatacenter
new DriverPropertyInfo("cacheConnectionsInDriver", "true"),
new DriverPropertyInfo("preparedStatementCacheQueries", "256"),
new DriverPropertyInfo("useQueryService", "true"),
new DriverPropertyInfo("useDiscovery", "true"),
new DriverPropertyInfo("usePrefixPath", ""),
new DriverPropertyInfo("localDatacenter", localDatacenter),
new DriverPropertyInfo("secureConnection", ""),
Expand Down Expand Up @@ -354,6 +355,7 @@ static DriverPropertyInfo[] customizedPropertyInfo() {
new DriverPropertyInfo("cacheConnectionsInDriver", "false"),
new DriverPropertyInfo("preparedStatementCacheQueries", "100"),
new DriverPropertyInfo("useQueryService", "false"),
new DriverPropertyInfo("useDiscovery", "false"),
new DriverPropertyInfo("usePrefixPath", "/demo/oltp"),
new DriverPropertyInfo("localDatacenter", "sas"),
new DriverPropertyInfo("secureConnection", "true"),
Expand Down