Skip to content

Table not exist only at read write splitting #35592

Open
@fengzao

Description

@fengzao

version: 5.5.2

project : ShardingSphere-JDBC

Expected behavior: return sql ref data

Actual behavior: throw exception with TableNotFoundException . "Exception in thread "main" org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view 'tbl_user' does not exist."

Reason analyze (If you can)

it seems the table exists check need skip ? when just use read write split

Example codes for reproduce this issue (such as a github link).

  • ALL CODE
public static void main(String[] args) throws IOException, SQLException {


        Map<String, DataSource> dataSourceMap = new HashMap<>();
        dataSourceMap.put("ds_0", initH2AndGetDatasource("db0"));
        dataSourceMap.put("ds_1", initH2AndGetDatasource("db1"));
        Map<String, AlgorithmConfiguration> balanceAlgorithmMap = new HashMap<>();
        balanceAlgorithmMap.put("random", new AlgorithmConfiguration("RANDOM", new Properties()));
        ReadwriteSplittingRuleConfiguration ruleConfiguration = new ReadwriteSplittingRuleConfiguration(
                Arrays.asList(
                        new ReadwriteSplittingDataSourceGroupRuleConfiguration("demo",
                                "ds_0",
                                Arrays.asList("ds_1"),
                                TransactionalReadQueryStrategy.PRIMARY,
                                "random")
                ), balanceAlgorithmMap);

        DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(
                new ModeConfiguration("Stdandalone", new StandalonePersistRepositoryConfiguration("JDBC", new Properties())),
                dataSourceMap,
                List.of(ruleConfiguration),
                new Properties());

        try (Connection connection = dataSource.getConnection()) {
            PreparedStatement statement = connection.prepareStatement("select max(id) from tbl_user");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                System.out.println("count = " + resultSet.getLong(1));
            }
        }


    }

    public static HikariDataSource initH2AndGetDatasource(String dbName) throws IOException, SQLException {
        HikariConfig config = new HikariConfig();
        config.setUsername("sa");
        config.setPassword("");
        config.setJdbcUrl("jdbc:h2:mem:" + dbName + ";MODE=MySQL;DB_CLOSE_DELAY=-1");

        HikariDataSource dataSource = new HikariDataSource(config);

        try (Connection connection = dataSource.getConnection()) {
            Statement statement = connection.createStatement();
            statement.execute("create table tbl_user(\n" +
                    "    id bigint primary key auto_increment,\n" +
                    "    name varchar(255),\n" +
                    "    age int\n" +
                    "\n" +
                    ");\n" +
                    "insert into tbl_user(name, age) values('zs', 30);\n" +
                    "insert into tbl_user(name, age) values('ls', 20);\n");
        }
        return dataSource;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions