Issue 1:
Bug Report

Development environment

Spring Boot: 2.5.4

MyBatis Spring Boot Starter: 2.2.0

ShardingSphere JDBC: 5.0.0-beta

MySQL: 8.0.23

Which version of ShardingSphere did you use?

5.0.0-beta

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

shardingsphere-jdbc-core

Expected behavior

Throw an exception.

The username field is unique, an exception should be thrown when adding duplicates. It is normal when not using ShardingSphere.

org.springframework.dao.DuplicateKeyException:
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'suikenung' for key 'user.uk_username'
### The error may exist in file [D:\Software\JetBrains\Projects\Java\sugus\sugus-system\target\classes\mapper\UserMapper.xml]
### The error may involve com.lrq.sugus.system.mapper.UserMapper.insert-Inline
### The error occurred while setting parameters
### SQL: insert into `user` ( username, `password`, enabled, full_name, email, mobile, sex ) values ( ?, ?, ?, ?, ?, ?, ? )
### Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'suikenung' for key 'user.uk_username'
; Duplicate entry 'suikenung' for key 'user.uk_username'; nested exception is java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'suikenung' for key 'user.uk_username'

Actual behavior

When using ShardingSphere, nothing happened. And no exception is thrown.

Adding dependent antlr4-runtime has no effect.

<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.2</version>
</dependency>

Reason analyze (If you can)

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

CREATE TABLE `user` (
...
UNIQUE INDEX `uk_username`(`username`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;

<insert id="insert" keyProperty="id" parameterType="com.lrq.sugus.system.dataobject.UserDO" useGeneratedKeys="true">
insert into `user` (username) values (#{username})
</insert>

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

Issue 2:
Hi,

If you're interested in this one, please comment below so I can assign it to u. :)

Background

OptimizeContextFactory is created in the constructor of StandardMetaDataContexts with metaDataMap.

If you dive into the constructor of OptimizeContextFactory and MetaDataContextsBuilder, you will find both handlings of them need loading table metadata. Therefore, this issue is to reduce the unnecessary and duplicated table metadata loading.

Expected result

Create OptimizeContextFactory in MetaDataContextsBuilder

Share the table metadata loading between OptimizeContextFactory and Map<String, ShardingSphereMetaData> of StandardMetaDataContexts

How

The entrance is buildSchema() of MetaDataContextsBuilder

Change the result of SchemaBuilder.build() to Map<ActualTableMetadata, LogicTableMetadata>

In SchemaBuilder,

addRuleConfiguredTables returns logicTableMetadata. On the other hand appendRemainTables(materials, result); returns actualTableMetadata. So we need to call load() and decorate() of TableMetaDataBuilder respectively to get the acutal and logic ones.

Assemble ShardingSphereSchema using the list of logic table metadata

Create OptimizeContextFactory by the list of actual table metadata

Remove the deprecated constructors of FederateSchemaMetadata and FederateTableMetadata