Skip to content

Commit c0c8725

Browse files
authored
optimize: optimize derivative product check base on mysql (#6044)
1 parent c829e73 commit c0c8725

4 files changed

Lines changed: 60 additions & 31 deletions

File tree

changes/en-us/develop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Add changes here for all PR submitted to the develop branch.
1212
- [[#6026](https://github.com/seata/seata/pull/6026)] fix incorrect metric report
1313

1414
### optimize:
15-
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] A brief and accurate description of PR
15+
- [[#6044](https://github.com/seata/seata/pull/6044)] optimize derivative product check base on mysql
1616

1717
### security:
1818
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] A brief and accurate description of PR

changes/zh-cn/develop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [[#6026](https://github.com/seata/seata/pull/6026)] 修复异常的打点
1313

1414
### optimize:
15-
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] 准确简要的PR描述
15+
- [[#6044](https://github.com/seata/seata/pull/6044)] 优化MySQL衍生数据库判断逻辑
1616

1717
### security:
1818
- [[#PR_NO](https://github.com/seata/seata/pull/PR_NO)] 准确简要的PR描述

rm-datasource/src/main/java/io/seata/rm/datasource/DataSourceProxy.java

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.sql.PreparedStatement;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22-
import java.sql.Statement;
2322

2423
import javax.sql.DataSource;
2524

@@ -31,6 +30,7 @@
3130
import io.seata.rm.datasource.sql.struct.TableMetaCacheFactory;
3231
import io.seata.rm.datasource.util.JdbcUtils;
3332
import io.seata.sqlparser.util.JdbcConstants;
33+
import org.apache.commons.lang.StringUtils;
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

@@ -55,7 +55,16 @@ public class DataSourceProxy extends AbstractDataSourceProxy implements Resource
5555

5656
private String userName;
5757

58-
private String version;
58+
private String kernelVersion;
59+
60+
private String productVersion;
61+
62+
/**
63+
* POLARDB-X 1.X -> TDDL
64+
* POLARDB-X 2.X & MySQL 5.6 -> PXC
65+
* POLARDB-X 2.X & MySQL 5.7 -> AliSQL-X
66+
*/
67+
private static final String[] POLARDB_X_PRODUCT_KEYWORD = {"TDDL","AliSQL-X","PXC"};
5968

6069
/**
6170
* Instantiates a new Data source proxy.
@@ -89,9 +98,9 @@ private void init(DataSource dataSource, String resourceGroupId) {
8998
if (JdbcConstants.ORACLE.equals(dbType)) {
9099
userName = connection.getMetaData().getUserName();
91100
} else if (JdbcConstants.MYSQL.equals(dbType)) {
92-
getMySQLAdaptiveType(connection);
101+
validMySQLVersion(connection);
102+
checkDerivativeProduct();
93103
}
94-
version = selectDbVersion(connection);
95104
} catch (SQLException e) {
96105
throw new IllegalStateException("can not init dataSource", e);
97106
}
@@ -103,17 +112,31 @@ private void init(DataSource dataSource, String resourceGroupId) {
103112
}
104113

105114
/**
106-
* get mysql adaptive type for PolarDB-X
115+
* Define derivative product version for MySQL Kernel
107116
*
108-
* @param connection db connection
109117
*/
110-
private void getMySQLAdaptiveType(Connection connection) {
111-
try (Statement statement = connection.createStatement()) {
112-
statement.executeQuery("show rule");
118+
private void checkDerivativeProduct() {
119+
if (!JdbcConstants.MYSQL.equals(dbType)) {
120+
return;
121+
}
122+
// check for polardb-x
123+
if (isPolardbXProduct()) {
113124
dbType = JdbcConstants.POLARDBX;
114-
} catch (SQLException e) {
115-
dbType = JdbcConstants.MYSQL;
125+
return;
126+
}
127+
// check for other products base on mysql kernel
128+
}
129+
130+
private boolean isPolardbXProduct() {
131+
if (StringUtils.isBlank(productVersion)) {
132+
return false;
133+
}
134+
for (String keyword : POLARDB_X_PRODUCT_KEYWORD) {
135+
if (productVersion.contains(keyword)) {
136+
return true;
137+
}
116138
}
139+
return false;
117140
}
118141

119142
/**
@@ -296,27 +319,33 @@ public BranchType getBranchType() {
296319
return BranchType.AT;
297320
}
298321

299-
public String getVersion() {
300-
return version;
322+
public String getKernelVersion() {
323+
return kernelVersion;
301324
}
302325

303-
private String selectDbVersion(Connection connection) {
304-
if (JdbcConstants.MYSQL.equals(dbType) || JdbcConstants.POLARDBX.equals(dbType)) {
305-
try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT VERSION()");
306-
ResultSet versionResult = preparedStatement.executeQuery()) {
307-
if (versionResult.next()) {
308-
String version = versionResult.getString("VERSION()");
309-
if (version == null) {
310-
return null;
311-
}
312-
int dashIdx = version.indexOf('-');
313-
// in mysql: 5.6.45, in polardb-x: 5.6.45-TDDL-xxx
314-
return dashIdx > 0 ? version.substring(0, dashIdx) : version;
326+
private void validMySQLVersion(Connection connection) {
327+
if (!JdbcConstants.MYSQL.equals(dbType)) {
328+
return;
329+
}
330+
try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT VERSION()");
331+
ResultSet versionResult = preparedStatement.executeQuery()) {
332+
if (versionResult.next()) {
333+
String version = versionResult.getString("VERSION()");
334+
if (StringUtils.isBlank(version)) {
335+
return;
336+
}
337+
int dashIdx = version.indexOf('-');
338+
// in mysql: 5.6.45, in polardb-x: 5.6.45-TDDL-xxx
339+
if (dashIdx > 0) {
340+
kernelVersion = version.substring(0, dashIdx);
341+
productVersion = version.substring(dashIdx + 1);
342+
} else {
343+
kernelVersion = version;
344+
productVersion = version;
315345
}
316-
} catch (Exception e) {
317-
LOGGER.error("get mysql version fail error: {}", e.getMessage());
318346
}
347+
} catch (Exception e) {
348+
LOGGER.error("check mysql version fail error: {}", e.getMessage());
319349
}
320-
return "";
321350
}
322351
}

rm-datasource/src/main/java/io/seata/rm/datasource/exec/mysql/MySQLUpdateJoinExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,6 @@ private String buildGroupBy(List<String> pkColumns,List<String> allSelectColumns
310310
}
311311

312312
private String getDbVersion() {
313-
return statementProxy.getConnectionProxy().getDataSourceProxy().getVersion();
313+
return statementProxy.getConnectionProxy().getDataSourceProxy().getKernelVersion();
314314
}
315315
}

0 commit comments

Comments
 (0)