AMashenkov commented on code in PR #2846: URL: https://github.com/apache/ignite-3/pull/2846#discussion_r1400344475
########## 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; Review Comment: ```suggestion /** Transaction, which is managed by SQL engine via {@link SqlQueryType#TX_CONTROL} statements. */ private volatile InternalTransaction managedTransaction; ``` -- 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]
