Skip to content

ShardingSphere-JDBC + Nacos causes Druid dbType initialization issue due to method mismatch #36182

@lly-ke

Description

@lly-ke

Bug Report

Which version of ShardingSphere did you use?

ShardingSphere-JDBC5.5.2

druid: 1.2.25

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-JDBC

Expected behavior

When using Nacos as the configuration center in Cluster mode, ShardingSphere-JDBC should correctly initialize the Druid DataSource and recognize the dbType field without any exceptions.

Actual behavior

With the following configuration:

mode:
  type: Cluster
  repository:
    type: Nacos

When using a Druid DataSource and setting dbType: "mysql" as a string, an exception occurs during DataSource initialization. The system seems to invoke the following method:

public void setDbType(DbType dbType)

But the actual value passed is a String ("mysql"), which leads to a type mismatch or runtime error.

Reason analyze (If you can)

When ShardingSphere loads the configuration from Nacos, the value of dbType is preserved as a String. However, during reflective invocation, it mistakenly calls:

public void setDbType(DbType dbType)

instead of:

public void setDbType(String dbType)

This causes incompatibility or failure in initializing the Druid DataSource.

Druid defines both overloaded methods:

public void setDbType(DbType dbType) {
    this.dbTypeName = dbType == null ? null : dbType.name();
}

public void setDbType(String dbType) {
    this.dbTypeName = dbType;
}

ShardingSphere incorrectly resolves the method to call when only a string value ("mysql") is provided through configuration center.

Steps to reproduce the behavior

  1. Use Druid as DataSource;
  2. Use Nacos as configuration repository with Cluster mode;
  3. Set dbType: "mysql" in the configuration;
  4. Start the application;
  5. Observe failure in DataSource initialization.

Example codes for reproduce this issue

Sample configuration:

mode:
  type: Cluster
  repository:
    type: Nacos

dataSources:
  ds_0:
    dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo_ds_0
    username: root
    password: root
    dbType: "mysql"

This leads to a type mismatch when calling setDbType(DbType) with a string value.

Image Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions