Skip to content

Commit

Permalink
bugfix: fix SQLServer-related SQL error when using database of SQLSer…
Browse files Browse the repository at this point in the history
…ver (#6493)
  • Loading branch information
TakeActionNow2019 authored Apr 26, 2024
1 parent e5639b4 commit 8c3224f
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 30 deletions.
6 changes: 2 additions & 4 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6465](https://github.com/apache/incubator-seata/pull/6465)] fix(6257): fix saga mode replay context lost start in 2.x
- [[#6469](https://github.com/apache/incubator-seata/pull/6469)] fix Error in insert sql of [lock_table] data table to sqlserver database
- [[#6492](https://github.com/apache/incubator-seata/pull/6492)] fix XA did not rollback but close when executing a long-running SQL(or deadlock SQL)
- [[#6493](https://github.com/apache/incubator-seata/pull/6493)] fix SQLServer-related SQL error in seata server when using database of SQLServer
- [[#6497](https://github.com/apache/incubator-seata/pull/6497)] fix tcc properties class when autoconfigure


### optimize:
- [[#6031](https://github.com/apache/incubator-seata/pull/6031)] add a check for the existence of the undolog table
- [[#6089](https://github.com/apache/incubator-seata/pull/6089)] modify the semantics of RaftServerFactory and remove unnecessary singleton
Expand Down Expand Up @@ -129,7 +129,6 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6412](https://github.com/apache/incubator-seata/pull/6412)] optimize core compatible module
- [[#6429](https://github.com/apache/incubator-seata/pull/6429)] remove repetitive words


### security:
- [[#6069](https://github.com/apache/incubator-seata/pull/6069)] Upgrade Guava dependencies to fix security vulnerabilities
- [[#6145](https://github.com/apache/incubator-seata/pull/6145)] upgrade jettison to 1.5.4
Expand All @@ -153,7 +152,6 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6466](https://github.com/apache/incubator-seata/pull/6466)] support redis integration testing
- [[#6484](https://github.com/apache/incubator-seata/pull/6484)] fix FileConfigurationTest and MockServerTest fail


### refactor:

- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] refactor Saga designer using diagram-js
Expand Down Expand Up @@ -193,6 +191,6 @@ Thanks to these contributors for their code commits. Please report an unintended
- [deung](https://github.com/deung)
- [tanyaofei](https://github.com/tanyaofei)
- [xjlgod](https://github.com/xjlgod)

- [TakeActionNow2019](https://github.com/TakeActionNow2019)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
5 changes: 2 additions & 3 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
- [[#6465](https://github.com/apache/incubator-seata/pull/6465)] 修复2.0下saga模式的context replay丢失start问题
- [[#6469](https://github.com/apache/incubator-seata/pull/6469)] 修复在sqlserver数据库下[lock_table]数据表的插入操作sql中存在的错误
- [[#6492](https://github.com/apache/incubator-seata/pull/6492)] 修复XA执行长时间SQL(或死锁SQL)没有完成回滚就释放连接
- [[#6493](https://github.com/apache/incubator-seata/pull/6493)] 修复当使用数据库为SQLServer时seata server的SQL报错
- [[#6497](https://github.com/apache/incubator-seata/pull/6497)] 修复自动装配时的seata tcc 配置类


### optimize:
- [[#6031](https://github.com/apache/incubator-seata/pull/6031)] 添加undo_log表的存在性校验
- [[#6089](https://github.com/apache/incubator-seata/pull/6089)] 修改RaftServerFactory语义并删除不必要的单例构建
Expand Down Expand Up @@ -150,7 +150,6 @@
- [[#6466](https://github.com/apache/incubator-seata/pull/6466)] 支持redis的集成测试
- [[#6484](https://github.com/apache/incubator-seata/pull/6484)] 修复FileConfigurationTest和MockServerTest失败


### refactor:

- [[#6280](https://github.com/apache/incubator-seata/pull/6280)] 使用diagram-js重构Saga设计器
Expand Down Expand Up @@ -190,6 +189,6 @@
- [deung](https://github.com/deung)
- [tanyaofei](https://github.com/tanyaofei)
- [xjlgod](https://github.com/xjlgod)
- [TakeActionNow2019](https://github.com/TakeActionNow2019)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public class PageUtil {
+ SOURCE_SQL_PLACE_HOLD + ") temp ) where rn between " + START_PLACE_HOLD + " and " + END_PLACE_HOLD;

/**
* The constant SQLSERVER_PAGE_TEMPLATE
* The constant SQLSERVER_PAGE_TEMPLATE. Currently, it only works for order-by condition of "ORDER BY gmt_create desc"
*/
private static final String SQLSERVER_PAGE_TEMPLATE = "select * from (select temp.*, ROW_NUMBER() OVER(ORDER BY (select NULL)) AS rowId from ("
private static final String SQLSERVER_PAGE_TEMPLATE = "select * from (select temp.*, ROW_NUMBER() OVER(ORDER BY gmt_create desc) AS rowId from ("
+ SOURCE_SQL_PLACE_HOLD + ") temp ) t where t.rowId between " + START_PLACE_HOLD + " and " + END_PLACE_HOLD;
/**
* check page parm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testPageSql() {
String oracleTargetSql = "select * from " +
"( select ROWNUM rn, temp.* from (select * from test where a = 1) temp )" +
" where rn between 1 and 5";
String sqlserverTargetSql = "select * from (select temp.*, ROW_NUMBER() OVER(ORDER BY (select NULL)) AS rowId from (select * from test where a = 1) temp ) t where t.rowId between 1 and 5";
String sqlserverTargetSql = "select * from (select temp.*, ROW_NUMBER() OVER(ORDER BY gmt_create desc) AS rowId from (select * from test where a = 1) temp ) t where t.rowId between 1 and 5";

assertEquals(PageUtil.pageSql(sourceSql, "mysql", 1, 5), mysqlTargetSql);
assertEquals(PageUtil.pageSql(sourceSql, "h2", 1, 5), mysqlTargetSql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/
package org.apache.seata.core.store.db.sql.distributed.lock;

import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.core.constants.ServerTableColumnsName;


@LoadLevel(name = "default")
public class BaseDistributedLockSql implements DistributedLockSql {
protected static final String DISTRIBUTED_LOCK_TABLE_PLACE_HOLD = " #distributed_lock_table# ";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.seata.core.store.db.sql.distributed.lock;

import org.apache.seata.common.loader.LoadLevel;
import org.apache.seata.core.constants.ServerTableColumnsName;

@LoadLevel(name = "sqlserver")
public class BaseDistributedLockSqlServer extends BaseDistributedLockSql {

protected static final String SELECT_FOR_UPDATE_SQL = "SELECT " + ALL_COLUMNS + " FROM " + DISTRIBUTED_LOCK_TABLE_PLACE_HOLD
+ " WITH (ROWLOCK, UPDLOCK, HOLDLOCK) WHERE " + ServerTableColumnsName.DISTRIBUTED_LOCK_KEY + " = ?";

@Override
public String getSelectDistributeForUpdateSql(String distributedLockTable) {
return SELECT_FOR_UPDATE_SQL.replace(DISTRIBUTED_LOCK_TABLE_PLACE_HOLD, distributedLockTable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,36 @@
package org.apache.seata.core.store.db.sql.distributed.lock;


import org.apache.seata.common.loader.EnhancedServiceLoader;
import org.apache.seata.common.loader.EnhancedServiceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DistributedLockSqlFactory {
private static final DistributedLockSql DISTRIBUTED_LOCK_SQL = new BaseDistributedLockSql();

private static final Logger LOGGER = LoggerFactory.getLogger(DistributedLockSqlFactory.class);

protected static Map<String, DistributedLockSql> distributedLockSqlCache = new ConcurrentHashMap<>(4);

/**
* get the lock store sql
*
* @param dbType the dbType, support mysql/oracle/h2/postgre/oceanbase/dm, it's useless now, but maybe useful later
* @param dbType the dbType, support mysql/oracle/h2/postgre/oceanbase/dm/sqlserver ...
* @return lock store sql
*/
public static DistributedLockSql getDistributedLogStoreSql(String dbType) {
return DISTRIBUTED_LOCK_SQL;
return distributedLockSqlCache.computeIfAbsent(dbType, method -> {
try {
return EnhancedServiceLoader.load(DistributedLockSql.class, dbType);
} catch (EnhancedServiceNotFoundException ex) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Can't special implementation of DistributedLockSql for {}", dbType);
}
}
return EnhancedServiceLoader.load(DistributedLockSql.class, "default");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ public class SqlServerLockStoreSql extends AbstractLockStoreSql {
private static final String INSERT_LOCK_SQL_SQLSERVER = "insert into " + LOCK_TABLE_PLACE_HOLD + "(" + ALL_COLUMNS + ")"
+ " values (?, ?, ?, ?, ?, ?, ?, SYSDATETIME(), SYSDATETIME(), ?)";

/**
* The constant QUERY_ALL_LOCK.
*/
private static final String QUERY_ALL_LOCK_SQLSERVER = "select " + ALL_COLUMNS + " from " + LOCK_TABLE_PLACE_HOLD
+ WHERE_PLACE_HOLD;

@Override
public String getInsertLockSQL(String lockTable) {
return INSERT_LOCK_SQL_SQLSERVER.replace(LOCK_TABLE_PLACE_HOLD, lockTable);
}

@Override
public String getAllLockSql(String lockTable, String whereCondition) {
return QUERY_ALL_LOCK_SQLSERVER.replace(LOCK_TABLE_PLACE_HOLD, lockTable).replace(WHERE_PLACE_HOLD, whereCondition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
@LoadLevel(name = "sqlserver")
public class SqlServerLogStoreSqls extends AbstractLogStoreSqls {

/**
* The constant QUERY_ALL_GLOBAL_SESSION_SQLSERVER.
*/
private static final String QUERY_ALL_GLOBAL_SESSION_SQLSERVER = "select " + ALL_GLOBAL_COLUMNS + " from "
+ GLOBAL_TABLE_PLACEHOLD + WHERE_PLACEHOLD;

/**
* The constant INSERT_GLOBAL_TRANSACTION_SQLSERVER.
*/
Expand Down Expand Up @@ -85,6 +91,11 @@ public class SqlServerLogStoreSqls extends AbstractLogStoreSqls {
+ " where " + ServerTableColumnsName.BRANCH_TABLE_XID + " = ?"
+ " and " + ServerTableColumnsName.BRANCH_TABLE_BRANCH_ID + " = ?";

@Override
public String getAllGlobalSessionSql(String globalTable, String whereCondition) {
return QUERY_ALL_GLOBAL_SESSION_SQLSERVER.replace(GLOBAL_TABLE_PLACEHOLD, globalTable).replace(WHERE_PLACEHOLD, whereCondition);
}

@Override
public String getInsertGlobalTransactionSQL(String globalTable) {
return INSERT_GLOBAL_TRANSACTION_SQLSERVER.replace(GLOBAL_TABLE_PLACEHOLD, globalTable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.apache.seata.core.store.db.sql.distributed.lock.BaseDistributedLockSql
org.apache.seata.core.store.db.sql.distributed.lock.BaseDistributedLockSqlServer
26 changes: 10 additions & 16 deletions script/server/db/sqlserver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,17 @@ CREATE NONCLUSTERED INDEX [idx_branch_id]
)
GO

CREATE NONCLUSTERED INDEX [idx_xid]
ON [branch_table] (
[xid]
)
GO

-- the table to store distributed lock constants
CREATE TABLE [distributed_lock]
(
[lock_key] VARCHAR(20) NOT NULL,
[lock_value] VARCHAR(20) NOT NULL,
[expire] bigint,
PRIMARY KEY CLUSTERED ([lock_key])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON);
)
[lock_key] char(20) not null primary key,
[lock_value] varchar(20) not null,
[expire] bigint
)
GO

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
INSERT INTO [distributed_lock] (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO [distributed_lock] (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO [distributed_lock] (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO [distributed_lock] (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
INSERT INTO [distributed_lock] (lock_key, lock_value, expire) VALUES ('UndologDelete', ' ', 0);

0 comments on commit 8c3224f

Please sign in to comment.