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

FMWK-267 Create driver configuration instance per connection #39

Merged
merged 4 commits into from
Nov 30, 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
101 changes: 55 additions & 46 deletions src/main/java/com/aerospike/jdbc/AerospikeConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.policy.Policy;
import com.aerospike.jdbc.model.DriverConfiguration;
import com.aerospike.jdbc.sql.SimpleWrapper;
import com.aerospike.jdbc.sql.type.ByteArrayBlob;
import com.aerospike.jdbc.sql.type.StringClob;
import com.aerospike.jdbc.util.URLParser;

import java.sql.*;
import java.util.Map;
Expand All @@ -17,30 +17,33 @@
import java.util.logging.Logger;

import static java.lang.String.format;
import static java.sql.ResultSet.*;
import static java.sql.ResultSet.CLOSE_CURSORS_AT_COMMIT;
import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT;
import static java.sql.ResultSet.TYPE_FORWARD_ONLY;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyMap;

public class AerospikeConnection implements Connection, SimpleWrapper {

private static final Logger logger = Logger.getLogger(AerospikeConnection.class.getName());
private static final String NOT_TRANSACTIONAL_MESSAGE = "Connection is not transactional";

private final String url;
private final DriverConfiguration config;
private final IAerospikeClient client;
private final AtomicReference<String> schema = new AtomicReference<>(null); // namespace
private volatile boolean readOnly = false;
private final Properties clientInfo = new Properties();
private volatile Map<String, Class<?>> typeMap = emptyMap();
private volatile int holdability = HOLD_CURSORS_OVER_COMMIT;
private final AtomicReference<String> schema = new AtomicReference<>(null); // namespace
private volatile boolean closed;

public AerospikeConnection(String url, Properties props) {
this.url = url;
URLParser.parseUrl(url, props);
client = new AerospikeClient(
URLParser.getClientPolicy(), URLParser.getHosts()
);
schema.set(URLParser.getSchema()); // namespace
config = new DriverConfiguration(props);
config.parse(url);
client = new AerospikeClient(config.getClientPolicy(), config.getHosts());
schema.set(config.getSchema()); // namespace
}

@Override
Expand All @@ -64,13 +67,13 @@ public String nativeSQL(String sql) throws SQLException {
}

@Override
public void setAutoCommit(boolean autoCommit) {
// do nothing
public boolean getAutoCommit() {
return true;
}

@Override
public boolean getAutoCommit() {
return true;
public void setAutoCommit(boolean autoCommit) {
// do nothing
}

@Override
Expand Down Expand Up @@ -100,6 +103,11 @@ public DatabaseMetaData getMetaData() {
return new AerospikeDatabaseMetadata(url, client, this);
}

@Override
public boolean isReadOnly() {
return readOnly;
}

@Override
public void setReadOnly(boolean readOnly) throws SQLException {
if (!isValid(1)) {
Expand All @@ -109,8 +117,8 @@ public void setReadOnly(boolean readOnly) throws SQLException {
}

@Override
public boolean isReadOnly() {
return readOnly;
public String getCatalog() {
return schema.get();
}

@Override
Expand All @@ -119,8 +127,8 @@ public void setCatalog(String catalog) {
}

@Override
public String getCatalog() {
return schema.get();
public int getTransactionIsolation() {
return TRANSACTION_NONE;
}

@Override
Expand All @@ -131,19 +139,14 @@ public void setTransactionIsolation(int level) throws SQLException {
}
}

@Override
public int getTransactionIsolation() {
return TRANSACTION_NONE;
}

@Override
public SQLWarning getWarnings() {
return null;
}

@Override
public void clearWarnings() {
// TODO make use of warnings
// TODO: make use of warnings
}

@Override
Expand All @@ -152,7 +155,8 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency) th
}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return prepareStatement(sql, resultSetType, resultSetConcurrency, holdability);
}

Expand All @@ -171,6 +175,11 @@ public void setTypeMap(Map<String, Class<?>> map) {
typeMap = map;
}

@Override
public int getHoldability() {
return holdability;
}

@Override
public void setHoldability(int holdability) throws SQLException {
if (isClosed()) {
Expand All @@ -187,33 +196,29 @@ public void setHoldability(int holdability) throws SQLException {
this.holdability = holdability;
}

@Override
public int getHoldability() {
return holdability;
}

@Override
public Savepoint setSavepoint() throws SQLException {
throw new SQLFeatureNotSupportedException("Connection is not transactional");
throw new SQLFeatureNotSupportedException(NOT_TRANSACTIONAL_MESSAGE);
}

@Override
public Savepoint setSavepoint(String name) throws SQLException {
throw new SQLFeatureNotSupportedException("Connection is not transactional");
throw new SQLFeatureNotSupportedException(NOT_TRANSACTIONAL_MESSAGE);
}

@Override
public void rollback(Savepoint savepoint) throws SQLException {
throw new SQLFeatureNotSupportedException("Connection is not transactional");
throw new SQLFeatureNotSupportedException(NOT_TRANSACTIONAL_MESSAGE);
}

@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
throw new SQLFeatureNotSupportedException("Connection is not transactional");
throw new SQLFeatureNotSupportedException(NOT_TRANSACTIONAL_MESSAGE);
}

@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
validateResultSetParameters(resultSetType, resultSetConcurrency, resultSetHoldability);
return new AerospikeStatement(client, this);
}
Expand All @@ -225,7 +230,8 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res
return new AerospikePreparedStatement(client, this, sql);
}

private void validateResultSetParameters(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
private void validateResultSetParameters(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
if (resultSetType != TYPE_FORWARD_ONLY) {
throw new SQLFeatureNotSupportedException("ResultSet type other than TYPE_FORWARD_ONLY is not supported");
}
Expand Down Expand Up @@ -287,22 +293,22 @@ public boolean isValid(int timeout) {

@Override
public void setClientInfo(String name, String value) {
clientInfo.setProperty(name, value);
config.put(name, value);
}

@Override
public void setClientInfo(Properties properties) {
clientInfo.putAll(properties);
public String getClientInfo(String name) {
return config.getClientInfo().getProperty(name);
}

@Override
public String getClientInfo(String name) {
return clientInfo.getProperty(name);
public Properties getClientInfo() {
return config.getClientInfo();
}

@Override
public Properties getClientInfo() {
return clientInfo;
public void setClientInfo(Properties properties) {
config.putAll(properties);
}

@Override
Expand All @@ -316,13 +322,13 @@ public Struct createStruct(String typeName, Object[] attributes) throws SQLExcep
}

@Override
public void setSchema(String schema) {
// do nothing
public String getSchema() throws SQLException {
return schema.get();
}

@Override
public String getSchema() {
return schema.get();
public void setSchema(String schema) {
// do nothing
}

@Override
Expand All @@ -347,4 +353,7 @@ public int getNetworkTimeout() {
return client.getReadPolicyDefault().totalTimeout;
}

public DriverConfiguration getConfiguration() {
return config;
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/aerospike/jdbc/AerospikeDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.aerospike.jdbc.sql.ListRecordSet;
import com.aerospike.jdbc.sql.SimpleWrapper;
import com.aerospike.jdbc.util.AerospikeUtils;
import com.aerospike.jdbc.util.URLParser;

import java.io.IOException;
import java.io.StringReader;
Expand Down Expand Up @@ -46,15 +45,14 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;

@SuppressWarnings("java:S1192")
public class AerospikeDatabaseMetadata implements DatabaseMetaData, SimpleWrapper {

private static final Logger logger = Logger.getLogger(AerospikeDatabaseMetadata.class.getName());
private static final String NEW_LINE = System.lineSeparator();

private final String url;
private final Properties clientInfo;
private final Connection connection;
private final InfoPolicy infoPolicy;
private final String dbBuild;
private final String dbEdition;
private final List<String> catalogs;
Expand All @@ -65,13 +63,12 @@ public AerospikeDatabaseMetadata(String url, IAerospikeClient client, Connection
logger.info("Init AerospikeDatabaseMetadata");
AerospikeSchemaBuilder.cleanSchemaCache();
this.url = url;
clientInfo = URLParser.getClientInfo();
this.connection = connection;
infoPolicy = client.getInfoPolicyDefault();

Collection<String> builds = synchronizedSet(new HashSet<>());
Collection<String> editions = synchronizedSet(new HashSet<>());
Collection<String> namespaces = synchronizedSet(new HashSet<>());
final InfoPolicy infoPolicy = client.getInfoPolicyDefault();
Arrays.stream(client.getNodes()).parallel()
.map(node -> Info.request(infoPolicy, node, "namespaces", "sets", "sindex", "build", "edition"))
.forEach(r -> {
Expand Down Expand Up @@ -121,8 +118,8 @@ public String getURL() {
}

@Override
public String getUserName() {
return clientInfo.getProperty("user");
public String getUserName() throws SQLException {
return connection.getClientInfo().getProperty("user");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AerospikePreparedStatement extends AerospikeStatement implements Pr
private final AerospikeQuery query;
private final Object[] parameterValues;

public AerospikePreparedStatement(IAerospikeClient client, Connection connection, String sql) {
public AerospikePreparedStatement(IAerospikeClient client, AerospikeConnection connection, String sql) {
super(client, connection);
this.sql = sql;
int params = parseParameters(sql, 0).getValue();
Expand All @@ -49,7 +49,8 @@ public AerospikePreparedStatement(IAerospikeClient client, Connection connection
} catch (SQLException e) {
throw new UnsupportedOperationException(e);
}
columns = AerospikeSchemaBuilder.getSchema(query.getSchemaTable(), client);
columns = AerospikeSchemaBuilder.getSchema(query.getSchemaTable(), client,
connection.getConfiguration().getScanPolicy());
}

@Override
Expand Down Expand Up @@ -143,6 +144,9 @@ public void setAsciiStream(int parameterIndex, InputStream x, int length) throws
setAsciiStream(parameterIndex, x, (long) length);
}

/**
* @deprecated Use {@code setCharacterStream}
*/
@Override
@Deprecated
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/aerospike/jdbc/AerospikeStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class AerospikeStatement implements Statement, SimpleWrapper {

private static final Logger logger = Logger.getLogger(AerospikeStatement.class.getName());
private static final String BATCH_NOT_SUPPORTED_MESSAGE = "Batch update is not supported";

protected final IAerospikeClient client;
private final Connection connection;
Expand All @@ -34,7 +35,7 @@ public class AerospikeStatement implements Statement, SimpleWrapper {
private ResultSet resultSet;
private int updateCount;

public AerospikeStatement(IAerospikeClient client, Connection connection) {
public AerospikeStatement(IAerospikeClient client, AerospikeConnection connection) {
this.client = client;
this.connection = connection;
try {
Expand Down Expand Up @@ -192,17 +193,17 @@ public int getResultSetType() {

@Override
public void addBatch(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException("Batch update is not supported");
throw new SQLFeatureNotSupportedException(BATCH_NOT_SUPPORTED_MESSAGE);
}

@Override
public void clearBatch() throws SQLException {
throw new SQLFeatureNotSupportedException("Batch update is not supported");
throw new SQLFeatureNotSupportedException(BATCH_NOT_SUPPORTED_MESSAGE);
}

@Override
public int[] executeBatch() throws SQLException {
throw new SQLFeatureNotSupportedException("Batch update is not supported");
throw new SQLFeatureNotSupportedException(BATCH_NOT_SUPPORTED_MESSAGE);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/aerospike/jdbc/async/RecordSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public boolean next()
interrupt();
}
if (keyRecord == FAILURE) {
logger.info(() -> String.format("timeoutMs: %d", timeoutMs));
throw new AerospikeException("Aerospike asynchronous command failure");
}
if (keyRecord == END) {
Expand Down
Loading
Loading