com.hazelcast.shaded.org.apache.calcite.avatica.AvaticaConnection Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.shaded.org.apache.calcite.avatica;
import com.hazelcast.shaded.org.apache.calcite.avatica.ColumnMetaData.AvaticaType;
import com.hazelcast.shaded.org.apache.calcite.avatica.ColumnMetaData.Rep;
import com.hazelcast.shaded.org.apache.calcite.avatica.Meta.ExecuteBatchResult;
import com.hazelcast.shaded.org.apache.calcite.avatica.Meta.MetaResultSet;
import com.hazelcast.shaded.org.apache.calcite.avatica.remote.KerberosConnection;
import com.hazelcast.shaded.org.apache.calcite.avatica.remote.Service;
import com.hazelcast.shaded.org.apache.calcite.avatica.remote.Service.ErrorResponse;
import com.hazelcast.shaded.org.apache.calcite.avatica.remote.Service.OpenConnectionRequest;
import com.hazelcast.shaded.org.apache.calcite.avatica.remote.TypedValue;
import com.hazelcast.shaded.org.apache.calcite.avatica.util.ArrayFactoryImpl;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Implementation of JDBC connection
* for the Avatica framework.
*
* Abstract to allow newer versions of JDBC to add methods.
*/
public abstract class AvaticaConnection implements Connection {
/** The name of the sole column returned by DML statements, containing
* the number of rows modified. */
public static final String ROWCOUNT_COLUMN_NAME = "ROWCOUNT";
//TODO shouldn't we move this to BuiltInConnectionProperty ?
public static final String NUM_EXECUTE_RETRIES_KEY = "avatica.statement.retries";
public static final String NUM_EXECUTE_RETRIES_DEFAULT = "5";
/** The name of the sole column returned by an EXPLAIN statement.
*
*
Actually Avatica does not care what this column is called, but here is
* a useful place to define a suggested value. */
public static final String PLAN_COLUMN_NAME = "PLAN";
public static final Helper HELPER = Helper.INSTANCE;
protected int statementCount;
private boolean closed;
private int holdability;
private int networkTimeout;
private KerberosConnection kerberosConnection;
private Service service;
public final String id;
public final Meta.ConnectionHandle handle;
protected final UnregisteredDriver driver;
protected final AvaticaFactory factory;
final String url;
protected final Properties info;
protected final Meta meta;
protected final AvaticaSpecificDatabaseMetaData metaData;
public final Map properties = new HashMap<>();
public final Map statementMap = new ConcurrentHashMap<>();
final Map flagMap = new ConcurrentHashMap<>();
protected final long maxRetriesPerExecute;
protected final boolean transparentReconnectEnabled;
/**
* Creates an AvaticaConnection.
*
* Not public; method is called only from the driver or a derived
* class.
*
* @param driver Driver
* @param factory Factory for JDBC objects
* @param url Server URL
* @param info Other connection properties
*/
protected AvaticaConnection(UnregisteredDriver driver,
AvaticaFactory factory,
String url,
Properties info) {
this.id = UUID.randomUUID().toString();
this.handle = new Meta.ConnectionHandle(this.id);
this.driver = driver;
this.factory = factory;
this.url = url;
this.info = info;
this.meta = driver.createMeta(this);
this.metaData = factory.newDatabaseMetaData(this);
try {
this.holdability = metaData.getResultSetHoldability();
} catch (SQLException e) {
// We know the impl doesn't throw this.
throw new RuntimeException(e);
}
this.maxRetriesPerExecute = getNumStatementRetries(info);
this.transparentReconnectEnabled = config().transparentReconnectionEnabled();
}
/** Computes the number of retries
* {@link AvaticaStatement#executeInternal(Meta.Signature, boolean)}
* should retry before failing. */
long getNumStatementRetries(Properties props) {
return Long.parseLong(Objects.requireNonNull(props)
.getProperty(NUM_EXECUTE_RETRIES_KEY, NUM_EXECUTE_RETRIES_DEFAULT));
}
/** Returns a view onto this connection's configuration properties. Code
* in Avatica and derived projects should use this view rather than calling
* {@link java.util.Properties#getProperty(String)}. Derived projects will
* almost certainly subclass {@link ConnectionConfig} with their own
* properties. */
public ConnectionConfig config() {
return new ConnectionConfigImpl(info);
}
/**
* Opens the connection on the server.
*/
public void openConnection() {
// Open the connection on the server
this.meta.openConnection(handle, OpenConnectionRequest.serializeProperties(info));
}
protected void checkOpen() throws SQLException {
if (isClosed()) {
throw HELPER.closed();
}
}
// Connection methods
public AvaticaStatement createStatement() throws SQLException {
checkOpen();
//noinspection MagicConstant
return createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY,
holdability);
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
checkOpen();
//noinspection MagicConstant
return prepareStatement(
sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
holdability);
}
public CallableStatement prepareCall(String sql) throws SQLException {
throw HELPER.unsupported();
}
public String nativeSQL(String sql) throws SQLException {
throw HELPER.unsupported();
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
checkOpen();
meta.connectionSync(handle, new ConnectionPropertiesImpl().setAutoCommit(autoCommit));
}
public boolean getAutoCommit() throws SQLException {
checkOpen();
return unbox(sync().isAutoCommit(), true);
}
public void commit() throws SQLException {
checkOpen();
meta.commit(handle);
}
public void rollback() throws SQLException {
checkOpen();
meta.rollback(handle);
}
public void close() throws SQLException {
if (!closed) {
closed = true;
// Per specification, if onConnectionClose throws, this method will throw
// a SQLException, but statement will still be closed.
try {
meta.closeConnection(handle);
driver.handler.onConnectionClose(this);
if (null != kerberosConnection) {
kerberosConnection.stopRenewalThread();
}
} catch (RuntimeException e) {
throw HELPER.createException("While closing connection", e);
}
}
}
public boolean isClosed() throws SQLException {
return closed;
}
public DatabaseMetaData getMetaData() throws SQLException {
checkOpen();
return metaData;
}
public void setReadOnly(boolean readOnly) throws SQLException {
checkOpen();
meta.connectionSync(handle, new ConnectionPropertiesImpl().setReadOnly(readOnly));
}
public boolean isReadOnly() throws SQLException {
checkOpen();
return unbox(sync().isReadOnly(), true);
}
public void setCatalog(String catalog) throws SQLException {
checkOpen();
meta.connectionSync(handle, new ConnectionPropertiesImpl().setCatalog(catalog));
}
public String getCatalog() throws SQLException {
checkOpen();
return sync().getCatalog();
}
public void setTransactionIsolation(int level) throws SQLException {
checkOpen();
meta.connectionSync(handle, new ConnectionPropertiesImpl().setTransactionIsolation(level));
}
public int getTransactionIsolation() throws SQLException {
checkOpen();
//noinspection MagicConstant
return unbox(sync().getTransactionIsolation(), TRANSACTION_NONE);
}
public SQLWarning getWarnings() throws SQLException {
checkOpen();
return null;
}
public void clearWarnings() throws SQLException {
checkOpen();
// no-op since connection pooling often calls this.
}
public Statement createStatement(
int resultSetType, int resultSetConcurrency) throws SQLException {
checkOpen();
//noinspection MagicConstant
return createStatement(resultSetType, resultSetConcurrency, holdability);
}
public PreparedStatement prepareStatement(
String sql,
int resultSetType,
int resultSetConcurrency) throws SQLException {
checkOpen();
//noinspection MagicConstant
return prepareStatement(
sql, resultSetType, resultSetConcurrency, holdability);
}
public CallableStatement prepareCall(
String sql,
int resultSetType,
int resultSetConcurrency) throws SQLException {
throw HELPER.unsupported();
}
public Map> getTypeMap() throws SQLException {
throw HELPER.unsupported();
}
public void setTypeMap(Map> map) throws SQLException {
throw HELPER.unsupported();
}
public void setHoldability(int holdability) throws SQLException {
checkOpen();
if (!(holdability == ResultSet.CLOSE_CURSORS_AT_COMMIT
|| holdability == ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
throw new SQLException("invalid value");
}
this.holdability = holdability;
}
public int getHoldability() throws SQLException {
checkOpen();
return holdability;
}
public Savepoint setSavepoint() throws SQLException {
throw HELPER.unsupported();
}
public Savepoint setSavepoint(String name) throws SQLException {
throw HELPER.unsupported();
}
public void rollback(Savepoint savepoint) throws SQLException {
throw HELPER.unsupported();
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
throw HELPER.unsupported();
}
public AvaticaStatement createStatement(
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
checkOpen();
return factory.newStatement(this, null, resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public PreparedStatement prepareStatement(
String sql,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
checkOpen();
try {
final Meta.StatementHandle h = meta.prepare(handle, sql, -1);
return factory.newPreparedStatement(this, h, h.signature, resultSetType,
resultSetConcurrency, resultSetHoldability);
} catch (RuntimeException e) {
throw HELPER.createException("while preparing SQL: " + sql, e);
}
}
public CallableStatement prepareCall(
String sql,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throw HELPER.unsupported();
}
public PreparedStatement prepareStatement(
String sql, int autoGeneratedKeys) throws SQLException {
throw HELPER.unsupported();
}
public PreparedStatement prepareStatement(
String sql, int[] columnIndexes) throws SQLException {
throw HELPER.unsupported();
}
public PreparedStatement prepareStatement(
String sql, String[] columnNames) throws SQLException {
throw HELPER.unsupported();
}
public Clob createClob() throws SQLException {
throw HELPER.unsupported();
}
public Blob createBlob() throws SQLException {
throw HELPER.unsupported();
}
public NClob createNClob() throws SQLException {
throw HELPER.unsupported();
}
public SQLXML createSQLXML() throws SQLException {
throw HELPER.unsupported();
}
public boolean isValid(int timeout) throws SQLException {
if (timeout < 0) {
throw HELPER.createException("timeout is less than 0");
}
// TODO check if connection is actually alive using timeout
return !isClosed();
}
public void setClientInfo(String name, String value)
throws SQLClientInfoException {
throw HELPER.clientInfo();
}
public void setClientInfo(Properties properties)
throws SQLClientInfoException {
throw HELPER.clientInfo();
}
public String getClientInfo(String name) throws SQLException {
return getClientInfo().getProperty(name);
}
public Properties getClientInfo() throws SQLException {
checkOpen();
return new Properties();
}
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
checkOpen();
@SuppressWarnings("unchecked")
List