This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1e4ee6eba49 Fix autocommit (#18001)
1e4ee6eba49 is described below
commit 1e4ee6eba49498900fd42ddaac45827b3d0ed347
Author: JingShang Lu <[email protected]>
AuthorDate: Fri May 27 16:06:21 2022 +0800
Fix autocommit (#18001)
* fix autocommit
* fix
---
.../transaction/utils/AutoCommitUtils.java | 45 ++++++++++++++++++++++
.../text/TextProtocolBackendHandlerFactory.java | 5 +--
2 files changed, 47 insertions(+), 3 deletions(-)
diff --git
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/utils/AutoCommitUtils.java
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/utils/AutoCommitUtils.java
new file mode 100644
index 00000000000..8f1975b5564
--- /dev/null
+++
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/utils/AutoCommitUtils.java
@@ -0,0 +1,45 @@
+/*
+ * 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.shardingsphere.transaction.utils;
+
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+
+/**
+ * Auto commit utils.
+ */
+public final class AutoCommitUtils {
+
+ /**
+ * Judge whether to start a new transaction.
+ *
+ * @param sqlStatement sqlStatement.
+ * @return need to open a new transaction.
+ */
+ public static boolean needOpenTransaction(final SQLStatement sqlStatement)
{
+ if (sqlStatement instanceof SelectStatement && null ==
((SelectStatement) sqlStatement).getFrom()) {
+ return false;
+ }
+ if (sqlStatement instanceof DDLStatement || sqlStatement instanceof
DMLStatement) {
+ return true;
+ }
+ return false;
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index 21d7302f04b..dd694ca28c8 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -47,13 +47,12 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.FlushStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dcl.DCLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.EmptyStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowCreateUserStatement;
+import org.apache.shardingsphere.transaction.utils.AutoCommitUtils;
import java.sql.SQLException;
import java.util.Collection;
@@ -141,7 +140,7 @@ public final class TextProtocolBackendHandlerFactory {
}
private static void handleAutoCommit(final SQLStatement sqlStatement,
final ConnectionSession connectionSession) throws SQLException {
- if (sqlStatement instanceof DDLStatement || sqlStatement instanceof
DMLStatement) {
+ if (AutoCommitUtils.needOpenTransaction(sqlStatement)) {
connectionSession.getBackendConnection().prepareForTaskExecution();
}
}