Skip to content

Commit

Permalink
Deprecate version parameter in JDBC URL
Browse files Browse the repository at this point in the history
  • Loading branch information
maximevw committed Oct 15, 2023
1 parent 5f9fda7 commit 7f2ad2f
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Changed
- Deprecate the parameter `version` (CQL version) in JDBC URL because this one is purely informational and has no
effect. This will be removed in the next release.

## [4.10.1] - 2023-10-07
### Changed
- Update Apache Commons IO to version 2.14.0.
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/com/ing/data/cassandra/jdbc/CassandraDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public class CassandraDataSource implements ConnectionPoolDataSource, DataSource
protected String password;
/**
* The CQL version.
* @deprecated For removal.
*/
@Deprecated
protected String version = null;
/**
* The consistency level.
Expand All @@ -107,9 +109,26 @@ public class CassandraDataSource implements ConnectionPoolDataSource, DataSource
* @param keyspace The keyspace.
* @param user The username used to connect.
* @param password The password used to connect.
* @param version The CQL version.
* @param consistency The consistency level.
*/
public CassandraDataSource(final String host, final int port, final String keyspace, final String user,
final String password, final String consistency) {
this(host, port, keyspace, user, password, null, consistency, null);
}

/**
* Constructor.
*
* @param host The host name.
* @param port The port.
* @param keyspace The keyspace.
* @param user The username used to connect.
* @param password The password used to connect.
* @param version The CQL version. Deprecated, do not use anymore.
* @param consistency The consistency level.
* @deprecated For removal. Use {@link #CassandraDataSource(String, int, String, String, String, String)} instead.
*/
@Deprecated
public CassandraDataSource(final String host, final int port, final String keyspace, final String user,
final String password, final String version, final String consistency) {
this(host, port, keyspace, user, password, version, consistency, null);
Expand All @@ -123,7 +142,7 @@ public CassandraDataSource(final String host, final int port, final String keysp
* @param keyspace The keyspace.
* @param user The username used to connect.
* @param password The password used to connect.
* @param version The CQL version.
* @param version The CQL version. Deprecated, do not use anymore.
* @param consistency The consistency level.
* @param localDataCenter The local datacenter.
*/
Expand Down Expand Up @@ -181,7 +200,10 @@ public void setServerName(final String serverName) {
* Gets the CQL version.
*
* @return The CQL version.
* @deprecated For removal.
*/
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public String getVersion() {
return this.version;
}
Expand All @@ -190,7 +212,10 @@ public String getVersion() {
* Sets the CQL version.
*
* @param version The CQL version.
* @deprecated For removal.
*/
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public void setVersion(final String version) {
this.version = version;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/ing/data/cassandra/jdbc/SessionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ private Session createSession(final Properties properties) throws SQLException {
configureSocketOptions(driverConfigLoaderBuilder, properties);

// Set credentials when applicable.
if (username.length() > 0) {
if (!username.isEmpty()) {
builder.withAuthCredentials(username, password);
}

// The DefaultLoadBalancingPolicy requires to specify a local data center.
builder.withLocalDatacenter(localDatacenter);
if (loadBalancingPolicy.length() > 0) {
if (!loadBalancingPolicy.isEmpty()) {
// if a custom load balancing policy has been given in the JDBC URL, parse it and add it to the cluster
// builder.
try {
Expand All @@ -282,7 +282,7 @@ private Session createSession(final Properties properties) throws SQLException {
}
}

if (retryPolicy.length() > 0) {
if (!retryPolicy.isEmpty()) {
// if retry policy has been given in the JDBC URL, parse it and add it to the cluster builder.
try {
driverConfigLoaderBuilder.withString(DefaultDriverOption.RETRY_POLICY_CLASS, retryPolicy);
Expand All @@ -294,7 +294,7 @@ private Session createSession(final Properties properties) throws SQLException {
}
}

if (reconnectPolicy.length() > 0) {
if (!reconnectPolicy.isEmpty()) {
// if reconnection policy has been given in the JDBC URL, parse it and add it to the cluster builder.
try {
final Map<DriverOption, Object> parsedPolicy = Optional.ofNullable(
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/ing/data/cassandra/jdbc/utils/JdbcUrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,29 @@ public final class JdbcUrlUtil {

/**
* JDBC URL parameter key for the CQL version.
* @deprecated For removal.
*/
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public static final String KEY_VERSION = "version";

/**
* Property name used to retrieve the active CQL version when the connection to Cassandra is established. This
* property is mapped from the JDBC URL parameter {@code version} or from the default value defined in the
* property {@code database.defaultCqlVersion} of the resource file 'jdbc-driver.properties'.
* @deprecated For removal, because {@link #KEY_VERSION} and {@link #TAG_CQL_VERSION} are deprecated.
*/
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public static final String TAG_ACTIVE_CQL_VERSION = "activeCqlVersion";

/**
* Property name used to retrieve the active CQL version when the connection to Cassandra is established. This
* property is mapped from the JDBC URL parameter {@code version}.
* @deprecated For removal.
*/
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public static final String TAG_CQL_VERSION = "cqlVersion";

/**
Expand Down Expand Up @@ -460,7 +469,7 @@ public static Properties parseURL(final String url) throws SQLException {
* Creates a "sub-name" portion of a JDBC URL from properties.
*
* @param props A {@link Properties} instance containing all the properties to be considered.
* @return A "sub-name" portion of a JDBC URL (for example: //myhost:9160/Test1?version=3.0.0).
* @return A "sub-name" portion of a JDBC URL (for example: //myhost:9160/Test1?localdatacenter=DC1).
* @throws SQLException when something went wrong during the "sub-name" creation.
* @throws SQLNonTransientConnectionException when the host name is missing.
*/
Expand Down Expand Up @@ -496,26 +505,18 @@ public static String createSubName(final Properties props) throws SQLException {
}

/**
* Builds the URI part containing the query parameters "consistency" and "version" from properties.
* Builds the URI part containing the query parameter "consistency" from properties.
*
* @param props A {@link Properties} instance containing all the properties to be considered.
* @return The URI part containing the query parameters (for example: "consistency=ONE&amp;version=3.0.0") or
* {@code null} if neither version nor consistency are defined in the provided properties.
* @return The URI part containing the query parameter "consistency" (for example: "consistency=ONE") or
* {@code null} if consistency is not defined in the provided properties.
*/
static String makeQueryString(final Properties props) {
final StringBuilder sb = new StringBuilder();
final String version = props.getProperty(TAG_CQL_VERSION);
final String consistency = props.getProperty(TAG_CONSISTENCY_LEVEL);
if (StringUtils.isNotBlank(consistency)) {
sb.append(KEY_CONSISTENCY).append("=").append(consistency);
}
if (StringUtils.isNotBlank(version)) {
if (sb.length() != 0) {
sb.append("&");
}
sb.append(KEY_VERSION).append("=").append(version);
}

if (sb.length() > 0) {
return sb.toString().trim();
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/jdbc-driver.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ driver.name=Cassandra JDBC Driver
driver.jdbcVersion=4.0

database.productName=Cassandra
# TODO: 'version' parameter is deprecated, this property has to be removed.
database.defaultCqlVersion=3.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class BatchStatementsUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
sqlConnection2 = newConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
sqlConnection2 = newConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CollectionsUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import javax.sql.DataSource;
import java.sql.SQLException;

import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CQL_VERSION;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CONSISTENCY_LEVEL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -30,22 +30,20 @@ class DataSourceUnitTest extends UsingCassandraContainerTest {
private static final String KEYSPACE = "test_keyspace";
private static final String USER = "testuser";
private static final String PASSWORD = "secret";
private static final String VERSION = "3.0.0";
private static final String CONSISTENCY = "ONE";

@Test
void givenParameters_whenConstructDataSource_returnCassandraDataSource() throws Exception {
final CassandraDataSource cds = new CassandraDataSource(
"localhost", 9042, KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY);
"localhost", 9042, KEYSPACE, USER, PASSWORD, CONSISTENCY);
assertEquals("localhost", cds.getServerName());
assertEquals(9042, cds.getPortNumber());
assertEquals(KEYSPACE, cds.getDatabaseName());
assertEquals(USER, cds.getUser());
assertEquals(PASSWORD, cds.getPassword());
assertEquals(VERSION, cds.getVersion());

final DataSource ds = new CassandraDataSource(cassandraContainer.getContactPoint().getHostName(),
cassandraContainer.getContactPoint().getPort(), KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY);
cassandraContainer.getContactPoint().getPort(), KEYSPACE, USER, PASSWORD, CONSISTENCY);
assertNotNull(ds);

// null username and password
Expand All @@ -58,7 +56,7 @@ void givenParameters_whenConstructDataSource_returnCassandraDataSource() throws
cnx = ds.getConnection();
assertFalse(cnx.isClosed());
ds.setLoginTimeout(5);
assertEquals(VERSION, ((CassandraConnection) cnx).getConnectionProperties().get(TAG_CQL_VERSION));
assertEquals(CONSISTENCY, ((CassandraConnection) cnx).getConnectionProperties().get(TAG_CONSISTENCY_LEVEL));
assertEquals(5, ds.getLoginTimeout());
}

Expand All @@ -67,7 +65,7 @@ void givenParameters_whenConstructDataSource_returnCassandraDataSource() throws
void givenCassandraDataSource_whenIsWrapperFor_returnExpectedValue() throws Exception {
final DataSource ds = new CassandraDataSource(
cassandraContainer.getContactPoint().getHostName(), cassandraContainer.getContactPoint().getPort(),
KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY);
KEYSPACE, USER, PASSWORD, CONSISTENCY);

// Assert it is a wrapper for DataSource.
assertTrue(ds.isWrapperFor(DataSource.class));
Expand All @@ -80,15 +78,15 @@ void givenCassandraDataSource_whenIsWrapperFor_returnExpectedValue() throws Exce
void givenCassandraDataSource_whenUnwrap_returnUnwrappedDatasource() throws Exception {
final DataSource ds = new CassandraDataSource(
cassandraContainer.getContactPoint().getHostName(), cassandraContainer.getContactPoint().getPort(),
KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY);
KEYSPACE, USER, PASSWORD, CONSISTENCY);
assertNotNull(ds.unwrap(DataSource.class));
}

@Test
void givenCassandraDataSource_whenUnwrapToInvalidInterface_throwException() {
final DataSource ds = new CassandraDataSource(
cassandraContainer.getContactPoint().getHostName(), cassandraContainer.getContactPoint().getPort(),
KEYSPACE, USER, PASSWORD, VERSION, CONSISTENCY);
KEYSPACE, USER, PASSWORD, CONSISTENCY);
assertThrows(SQLException.class, () -> ds.unwrap(this.getClass()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class JdbcRegressionUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");

// Update cluster name according to the configured name.
/* try (final Statement statement = sqlConnection.createStatement()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class JsonSupportUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MetadataResultSetsUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PreparedStatementsUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ResultSetUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@Test
Expand Down
11 changes: 3 additions & 8 deletions src/test/java/com/ing/data/cassandra/jdbc/UtilsUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CLOUD_SECURE_CONNECT_BUNDLE;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CONNECTION_RETRIES;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CONSISTENCY_LEVEL;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_CQL_VERSION;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_DATABASE_NAME;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_DEBUG;
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_LOAD_BALANCING_POLICY;
Expand Down Expand Up @@ -84,36 +83,32 @@ static Stream<Arguments> buildUrlParsingTestCases() {
put(TAG_USER, "user1");
put(TAG_PASSWORD, "password1");
}}),
Arguments.of("jdbc:cassandra://localhost:9042/Keyspace1?version=3.0.0&consistency=QUORUM",
Arguments.of("jdbc:cassandra://localhost:9042/Keyspace1?consistency=QUORUM",
new HashMap<String, String>() {{
put(TAG_SERVER_NAME, "localhost");
put(TAG_PORT_NUMBER, "9042");
put(TAG_DATABASE_NAME, "Keyspace1");
put(TAG_CQL_VERSION, "3.0.0");
put(TAG_CONSISTENCY_LEVEL, "QUORUM");
}}),
Arguments.of("jdbc:cassandra://localhost/Keyspace1?consistency=QUORUM",
new HashMap<String, String>() {{
put(TAG_SERVER_NAME, "localhost");
put(TAG_PORT_NUMBER, "9042");
put(TAG_DATABASE_NAME, "Keyspace1");
put(TAG_CQL_VERSION, null);
put(TAG_CONSISTENCY_LEVEL, "QUORUM");
}}),
Arguments.of("jdbc:cassandra://localhost/Keyspace1?version=2.0.0",
Arguments.of("jdbc:cassandra://localhost/Keyspace1",
new HashMap<String, String>() {{
put(TAG_SERVER_NAME, "localhost");
put(TAG_PORT_NUMBER, "9042");
put(TAG_DATABASE_NAME, "Keyspace1");
put(TAG_CQL_VERSION, "2.0.0");
put(TAG_CONSISTENCY_LEVEL, null);
}}),
Arguments.of("jdbc:cassandra://localhost",
new HashMap<String, String>() {{
put(TAG_SERVER_NAME, "localhost");
put(TAG_PORT_NUMBER, "9042");
put(TAG_DATABASE_NAME, null);
put(TAG_CQL_VERSION, null);
put(TAG_CONSISTENCY_LEVEL, null);
}}),
Arguments.of("jdbc:cassandra://localhost/Keyspace1?localdatacenter=DC1",
Expand Down Expand Up @@ -194,7 +189,7 @@ void givenReconnectionPolicyString_whenParsePolicy_returnExpectedOptions(

@Test
void testCreateSubName() throws Exception {
final String jdbcUrl = "jdbc:cassandra://localhost:9042/Keyspace1?consistency=QUORUM&version=3.0.0";
final String jdbcUrl = "jdbc:cassandra://localhost:9042/Keyspace1?consistency=QUORUM";
final Properties props = parseURL(jdbcUrl);
final String result = createSubName(props);
assertEquals(jdbcUrl, PROTOCOL + result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VectorsDseContainerTest extends UsingDseContainerTest {
@BeforeAll
static void finalizeSetUpTests() throws Exception {
initializeContainer();
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class VectorsUnitTest extends UsingCassandraContainerTest {

@BeforeAll
static void finalizeSetUpTests() throws Exception {
initConnection(KEYSPACE, "version=3.0.0", "localdatacenter=datacenter1");
initConnection(KEYSPACE, "localdatacenter=datacenter1");
}

@Test
Expand Down

0 comments on commit 7f2ad2f

Please sign in to comment.