AMashenkov commented on a change in pull request #284: URL: https://github.com/apache/ignite-3/pull/284#discussion_r700068482
########## File path: modules/client/src/main/java/org/apache/ignite/jdbc/JdbcConnection.java ########## @@ -0,0 +1,753 @@ +/* + * 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 org.apache.ignite.jdbc; + +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.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLPermission; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.ShardingKey; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Arrays; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.Executor; +import org.apache.ignite.client.IgniteClient; +import org.apache.ignite.client.proto.query.JdbcQueryEventHandler; +import org.apache.ignite.client.proto.query.SqlStateCode; +import org.apache.ignite.internal.client.HostAndPortRange; +import org.apache.ignite.internal.client.TcpIgniteClient; +import org.apache.ignite.internal.client.query.JdbcClientQueryEventHandler; +import org.jetbrains.annotations.Nullable; + +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 org.apache.ignite.client.proto.query.SqlStateCode.CONNECTION_CLOSED; + +/** + * JDBC connection implementation. + */ +public class JdbcConnection implements Connection { + /** Default schema. */ + public static final String DFLT_SCHEMA = "PUBLIC"; + + /** Network timeout permission. */ + private static final String SET_NETWORK_TIMEOUT_PERM = "setNetworkTimeout"; + + /** Statements modification mutex. */ + private final Object stmtsMux = new Object(); + + /** Handler. */ + private final JdbcQueryEventHandler handler; + + /** Schema name. */ + private String schema; + + /** Closed flag. */ + private volatile boolean closed; + + /** Current transaction isolation. */ + private int txIsolation; + + /** Auto-commit flag. */ + private boolean autoCommit; + + /** Read-only flag. */ + private boolean readOnly; + + /** Current transaction holdability. */ + private int holdability; + + /** Connection properties. */ + private final ConnectionProperties connProps; + + /** Tracked statements to close on disconnect. */ + private final Set<JdbcStatement> stmts = Collections.newSetFromMap(new IdentityHashMap<>()); + + /** Network timeout. */ + private int netTimeout; + + /** Query timeout. */ + private final @Nullable Integer qryTimeout; + + /** Ignite remote client. */ + private final TcpIgniteClient client; + + /** + * Constructor. + * + * @param handler Handler. + * @param props Properties. + */ + public JdbcConnection(JdbcQueryEventHandler handler, ConnectionProperties props) { + this.connProps = props; + this.handler = handler; + + autoCommit = true; + + netTimeout = connProps.getConnectionTimeout(); + qryTimeout = connProps.getQueryTimeout(); + + holdability = HOLD_CURSORS_OVER_COMMIT; + + schema = DFLT_SCHEMA; + + client = null; + } + + /** + * Creates new connection. + * + * @param props Connection properties. + */ + public JdbcConnection(ConnectionProperties props) { + this.connProps = props; + autoCommit = true; + + var objects = Arrays.stream(props.getAddresses()).map(HostAndPortRange::toString).toArray(String[]::new); + + client = ((TcpIgniteClient)IgniteClient + .builder() + .addresses(objects) + .build()); + + this.handler = new JdbcClientQueryEventHandler(client); + + txIsolation = Connection.TRANSACTION_NONE; + + schema = normalizeSchema(connProps.getSchema()); + + netTimeout = connProps.getConnectionTimeout(); + qryTimeout = connProps.getQueryTimeout(); + + holdability = HOLD_CURSORS_OVER_COMMIT; + } + + /** {@inheritDoc} */ + @Override public Statement createStatement() throws SQLException { + return createStatement(TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, HOLD_CURSORS_OVER_COMMIT); + } + + /** {@inheritDoc} */ + @Override public Statement createStatement(int resSetType, int resSetConcurrency) throws SQLException { + return createStatement(resSetType, resSetConcurrency, HOLD_CURSORS_OVER_COMMIT); + } + + /** {@inheritDoc} */ + @Override public Statement createStatement(int resSetType, int resSetConcurrency, + int resSetHoldability) throws SQLException { + ensureNotClosed(); + + checkCursorOptions(resSetType, resSetConcurrency); + + JdbcStatement stmt = new JdbcStatement(this, resSetHoldability, schema); + + if (qryTimeout != null) + stmt.setQueryTimeout(qryTimeout); + + synchronized (stmtsMux) { + stmts.add(stmt); + } + + return stmt; + } + + /** {@inheritDoc} */ + @Override public PreparedStatement prepareStatement(String sql) throws SQLException { + return prepareStatement(sql, TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, HOLD_CURSORS_OVER_COMMIT); + } + + /** {@inheritDoc} */ + @Override public PreparedStatement prepareStatement(String sql, int resSetType, + int resSetConcurrency) throws SQLException { + return prepareStatement(sql, resSetType, resSetConcurrency, HOLD_CURSORS_OVER_COMMIT); + } + + /** {@inheritDoc} */ + @Override public PreparedStatement prepareStatement(String sql, int resSetType, int resSetConcurrency, + int resSetHoldability) throws SQLException { + ensureNotClosed(); + + checkCursorOptions(resSetType, resSetConcurrency); + + if (sql == null) + throw new SQLException("SQL string cannot be null."); Review comment: ```suggestion Objects.requireNonNull(sql); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org