xtern commented on code in PR #2846: URL: https://github.com/apache/ignite-3/pull/2846#discussion_r1400388258
########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/QueryTransactionHandler.java: ########## @@ -0,0 +1,192 @@ +/* + * 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.internal.sql.engine; + +import static org.apache.ignite.lang.ErrorGroups.Sql.RUNTIME_ERR; + +import org.apache.calcite.sql.SqlNode; +import org.apache.ignite.internal.sql.engine.sql.IgniteSqlCommitTransaction; +import org.apache.ignite.internal.sql.engine.sql.IgniteSqlStartTransaction; +import org.apache.ignite.internal.sql.engine.sql.IgniteSqlStartTransactionMode; +import org.apache.ignite.internal.sql.engine.sql.ParsedResult; +import org.apache.ignite.internal.tx.InternalTransaction; +import org.apache.ignite.sql.ExternalTransactionNotSupportedException; +import org.apache.ignite.sql.SqlException; +import org.apache.ignite.tx.IgniteTransactions; +import org.apache.ignite.tx.TransactionOptions; +import org.jetbrains.annotations.Nullable; + +/** + * The query transaction handler is responsible for running implicit or script managed transactions during query execution. + */ +@SuppressWarnings("InterfaceMayBeAnnotatedFunctional") +public interface QueryTransactionHandler { + /** + * Starts a transaction if there is no external transaction. + * + * @param parsedResult Result of the parse. + * @return Transaction wrapper. + */ + QueryTransactionWrapper startTxIfNeeded(ParsedResult parsedResult); + + /** + * Creates a new transaction handler that starts an implicit transaction if there is no external transaction. + * + * @param transactions Ignite transactions facade. + * @param externalTransaction External transaction. + * @return Transaction handler. + */ + static QueryTransactionHandler forSingleStatement(IgniteTransactions transactions, @Nullable InternalTransaction externalTransaction) { + return new QueryTransactionHandlerImpl(transactions, externalTransaction); + } + + /** + * Creates a new transaction handler that starts an implicit transaction if there is no external transaction and + * supports script transaction management using {@link SqlQueryType#TX_CONTROL} statements. + * + * @param transactions Ignite transactions facade. + * @param externalTransaction External transaction. + * @return Transaction handler. + */ + static QueryTransactionHandler forMultiStatement(IgniteTransactions transactions, @Nullable InternalTransaction externalTransaction) { + return new ControlStatementAwareTransactionHandler(transactions, externalTransaction); + } + + /** + * Starts an implicit transaction if there is no external transaction. + */ + class QueryTransactionHandlerImpl implements QueryTransactionHandler { + final IgniteTransactions transactions; + final @Nullable InternalTransaction externalTransaction; + + QueryTransactionHandlerImpl(IgniteTransactions transactions, @Nullable InternalTransaction externalTransaction) { + this.transactions = transactions; + this.externalTransaction = externalTransaction; + } + + protected @Nullable InternalTransaction activeTransaction() { + return externalTransaction; + } + + @Override + public QueryTransactionWrapper startTxIfNeeded(ParsedResult parsedResult) { + SqlQueryType queryType = parsedResult.queryType(); + InternalTransaction activeTx = activeTransaction(); + + if (activeTx == null) { + return new QueryTransactionWrapper((InternalTransaction) transactions.begin( + new TransactionOptions().readOnly(queryType != SqlQueryType.DML)), true); + } + + if (SqlQueryType.DDL == queryType) { + throw new SqlException(RUNTIME_ERR, "DDL doesn't support transactions."); + } + + if (SqlQueryType.DML == queryType && activeTx.isReadOnly()) { + throw new SqlException(RUNTIME_ERR, "DML query cannot be started by using read only transactions."); + } + + return new QueryTransactionWrapper(activeTx, false); + } + } + + /** + * Starts an implicit transaction if there is no external transaction. + * Supports script transaction management using {@link SqlQueryType#TX_CONTROL} statements. + */ + class ControlStatementAwareTransactionHandler extends QueryTransactionHandlerImpl { + private static final NoopTransactionWrapper noopTxWrapper = new NoopTransactionWrapper(); + private volatile InternalTransaction scriptManagedTransaction; + + ControlStatementAwareTransactionHandler(IgniteTransactions transactions, @Nullable InternalTransaction externalTransaction) { + super(transactions, externalTransaction); + } + + @Override + protected @Nullable InternalTransaction activeTransaction() { + return externalTransaction == null ? scriptManagedTransaction : externalTransaction; + } + + @Override + public QueryTransactionWrapper startTxIfNeeded(ParsedResult parsedResult) { + try { + SqlQueryType queryType = parsedResult.queryType(); + + if (queryType == SqlQueryType.TX_CONTROL) { + if (externalTransaction != null) { + throw new ExternalTransactionNotSupportedException(); Review Comment: Actually, this needs some clarification. We don’t have a native api at all to pass an external transaction. This case is only possible in jdbc (for now). And the point here is not that we starting a nested transaction, but that our jdbc non-autocommit mode implementation cannot be integrated with a transaction created in the script. For example: ``` connection.setAutoCommit(false) ... execute("INSERT..."); execute("SELECT..."); connection.commit(); ``` Jdbc handler implicitly starts `external` transaction and commits it when user call `connection.commit()`. This is ok, but what with the following ``` connection.setAutoCommit(false) execute("START TRANSACTION" + "INSERT..." + "COMMIT; + "START TRANSACTION" + "INSERT... connection.commit() ``` Our jdbc implementation is not aware at all about transaction started in script, so user cannot commit it by calling "connection.commit()" So for the consistency "START TRANSACTION/COMMIT" are forbidden for autocommit=false jdbc mode. That's way we must throw this exception even for single commit statement, for example: ``` queryScript(...tx, "COMMIT;") ``` In this case, we are not talking about an nested transaction. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
