diff --git a/jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java b/jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java index 7fe4885..a4a5493 100644 --- a/jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java +++ b/jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java @@ -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; @@ -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) diff --git a/jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java b/jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java index e6fcbe7..24ba353 100644 --- a/jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java +++ b/jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java @@ -42,6 +42,9 @@ public class YdbConfig { static final YdbProperty USE_PREFIX_PATH = YdbProperty.string("usePrefixPath", "Add prefix path to all operations performed by driver"); + static final YdbProperty USE_DISCOVERY = YdbProperty.bool("useDiscovery", + "Use discovery (client balancing) for YDB cluster connection", true); + static final YdbProperty FULLSCAN_DETECTOR_ENABLED = YdbProperty.bool( "jdbcFullScanDetector", "Enable analizator for collecting query stats", false ); @@ -64,6 +67,7 @@ public class YdbConfig { private final int preparedStatementsCacheSize; private final boolean useQueryService; + private final boolean useDiscovery; private final YdbValue usePrefixPath; private final boolean fullScanDetectorEnabled; @@ -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(); @@ -124,6 +129,10 @@ public boolean isUseQueryService() { return this.useQueryService; } + public boolean isUseDiscovery() { + return this.useDiscovery; + } + public boolean hasPrefixPath() { return usePrefixPath.hasValue(); } @@ -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), diff --git a/jdbc/src/test/java/tech/ydb/jdbc/YdbDriverExampleTest.java b/jdbc/src/test/java/tech/ydb/jdbc/YdbDriverExampleTest.java index 8834b8e..8de0d0d 100644 --- a/jdbc/src/test/java/tech/ydb/jdbc/YdbDriverExampleTest.java +++ b/jdbc/src/test/java/tech/ydb/jdbc/YdbDriverExampleTest.java @@ -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; @@ -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"); @@ -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) { @@ -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(); + } } diff --git a/jdbc/src/test/java/tech/ydb/jdbc/settings/YdbDriverProperitesTest.java b/jdbc/src/test/java/tech/ydb/jdbc/settings/YdbDriverProperitesTest.java index 8cec5c0..4e20f64 100644 --- a/jdbc/src/test/java/tech/ydb/jdbc/settings/YdbDriverProperitesTest.java +++ b/jdbc/src/test/java/tech/ydb/jdbc/settings/YdbDriverProperitesTest.java @@ -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", ""), @@ -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"),