1919import java .sql .PreparedStatement ;
2020import java .sql .ResultSet ;
2121import java .sql .SQLException ;
22- import java .sql .Statement ;
2322
2423import javax .sql .DataSource ;
2524
3130import io .seata .rm .datasource .sql .struct .TableMetaCacheFactory ;
3231import io .seata .rm .datasource .util .JdbcUtils ;
3332import io .seata .sqlparser .util .JdbcConstants ;
33+ import org .apache .commons .lang .StringUtils ;
3434import org .slf4j .Logger ;
3535import 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}
0 commit comments