Skip to content

Commit

Permalink
Fix #67: connection issues to AstraDB
Browse files Browse the repository at this point in the history
Fix issues when connecting without hostname and using dedicated 'dbaas' protocol
  • Loading branch information
maximevw committed Jul 1, 2024
1 parent 1941916 commit e787743
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- throws a `BatchUpdateException` when at least one statement in the batch fails or attempts to return a result set.
- the returned array contains `SUCCESS_NO_INFO` for successful statements and `EXECUTE_FAILED` for statements that
threw a `BatchUpdateException`.
- Fix connection issues to AstraDB when using protocol `jdbc:cassandra:dbaas` without specifying a hostname (see issue
[#67](https://github.com/ing-bank/cassandra-jdbc-wrapper/issues/67)).

## [4.12.0] - 2024-05-05
### Added
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/ing/data/cassandra/jdbc/SessionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ private Session createSession(final Properties properties) throws SQLException {
}
}

final List<ContactPoint> contactPoints = (List<ContactPoint>) properties.get(TAG_CONTACT_POINTS);
final List<ContactPoint> contactPoints = (List<ContactPoint>) properties.getOrDefault(TAG_CONTACT_POINTS,
new ArrayList<>());
final String cloudSecureConnectBundle = properties.getProperty(TAG_CLOUD_SECURE_CONNECT_BUNDLE);
final String keyspace = properties.getProperty(TAG_DATABASE_NAME);
final String username = properties.getProperty(TAG_USER, StringUtils.EMPTY);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/ing/data/cassandra/jdbc/utils/JdbcUrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ public final class JdbcUrlUtil {
*/
public static final String KEY_ACTIVE_PROFILE = "activeprofile";

/**
* Property name used to determine if the current connection is established to a cloud database. In such a case,
* the hostname can be ignored.
* This property is mapped from the JDBC URL protocol (see {@link #PROTOCOL_DBAAS}).
*/
public static final String TAG_DBAAS_CONNECTION = "isDbaasConnection";

static final Logger LOG = LoggerFactory.getLogger(JdbcUrlUtil.class);

private static final String HOST_SEPARATOR = "--";
Expand Down Expand Up @@ -356,6 +363,7 @@ public static Properties parseURL(final String url) throws SQLException {
if (url.startsWith(PROTOCOL_DBAAS)) {
uriStartIndex = PROTOCOL_DBAAS.length();
isDbaasConnection = true;
props.put(TAG_DBAAS_CONNECTION, true);
}
final String rawUri = url.substring(uriStartIndex);
final URI uri;
Expand Down Expand Up @@ -531,7 +539,8 @@ public static String createSubName(final Properties props) throws SQLException {
.map(ContactPoint::toString)
.collect(Collectors.joining(HOST_SEPARATOR));
}
if (hostsAndPorts == null) {
final boolean isDbaasConnection = (boolean) props.getOrDefault(TAG_DBAAS_CONNECTION, false);
if (hostsAndPorts == null && !isDbaasConnection) {
throw new SQLNonTransientConnectionException(HOST_REQUIRED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static void setupAstra() throws Exception {
* Note: Astra can be accessed with only a token (username='token').
*/
sqlConnection = (CassandraConnection) DriverManager.getConnection(
"jdbc:cassandra://dbaas/" + KEYSPACE_NAME +
"jdbc:cassandra:dbaas:///" + KEYSPACE_NAME +
"?user=" + "token" +
"&password=" + TestUtils.getAstraToken() + // env var ASTRA_DB_APPLICATION_TOKEN
"&consistency=" + "LOCAL_QUORUM" +
Expand Down

0 comments on commit e787743

Please sign in to comment.