standbyside opened a new issue, #30525:
URL: https://github.com/apache/shardingsphere/issues/30525
## Bug Report
### Which version of ShardingSphere did you use?
5.4.1
### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
I used mix mode, ShardingSphere-JDBC and ShardingSphere-Proxy.
### Expected behavior
I insert a data to t_user, and then, I throw a runtime excception. I hope
that transaction can be rollback when exception occur. I don' know if I use the
wrong version, or I used in the wrong way. Should I upgrade any jar's version?
Please tell me how to fix it. Thanks.
```
@TableName("t_user")
public class TUser {
private Long id;
private String name;
}
@Transactional(rollbackFor = Exception.class)
@GetMapping("/test")
public void test(@RequestParam("userId") Long userId) {
TUser user = new TUser();
user.setId(userId);
user.setName("user" + userId);
userMapper.insert(user);
throw new RuntimeException("rollback exception");
}
```
### Actual behavior
The log print "rollbacked", but the data is still in table.
```
[example-admin-test1:8781::] 2024-03-18 09:59:53.326:[ INFO] [18984] []
[XNIO-2 task-1:104078] [ShardingSphere-SQL.log:73] Logic SQL: INSERT INTO
t_user ( id, name ) VALUES ( ?, ? )
[example-admin-test1:8781::] 2024-03-18 09:59:53.326:[ INFO] [18984] []
[XNIO-2 task-1:104078] [ShardingSphere-SQL.log:73] Actual SQL: ds_user :::
INSERT INTO t_user ( id, name ) VALUES (?, ?) ::: [2, user2]
[example-admin-test1:8781::] 2024-03-18 09:59:53.431:[ INFO] [18984] []
[rpcDispatch_RMROLE_1_1_16:104183]
[i.s.c.r.processor.client.RmBranchRollbackProcessor.process:56] rm handle
branch rollback
process:xid=10.168.2.140:8091:135607899824493054,branchId=135607899824493056,branchType=AT,resourceId=jdbc:mysql://10.168.2.140:3306/ds_user,applicationData=null
[example-admin-test1:8781::] 2024-03-18 09:59:53.434:[ INFO] [18984] []
[rpcDispatch_RMROLE_1_1_16:104186]
[io.seata.rm.AbstractRMHandler.doBranchRollback:123] Branch Rollbacking:
10.168.2.140:8091:135607899824493054 135607899824493056
jdbc:mysql://10.168.2.140:3306/ds_user
[example-admin-test1:8781::] 2024-03-18 09:59:57.075:[ INFO] [18984] []
[rpcDispatch_RMROLE_1_1_16:107827]
[io.seata.rm.datasource.undo.AbstractUndoLogManager.undo:340] xid
10.168.2.140:8091:135607899824493054 branch 135607899824493056, undo_log added
with GlobalFinished
[example-admin-test1:8781::] 2024-03-18 09:59:57.077:[ INFO] [18984] []
[rpcDispatch_RMROLE_1_1_16:107829]
[io.seata.rm.AbstractRMHandler.doBranchRollback:131] Branch Rollbacked result:
PhaseTwo_Rollbacked
[example-admin-test1:8781::] 2024-03-18 09:59:57.088:[ INFO] [18984] []
[XNIO-2 task-1:107840] [io.seata.tm.api.DefaultGlobalTransaction.suspend:188]
Suspending current transaction, xid = 10.168.2.140:8091:135607899824493054
[example-admin-test1:8781::] 2024-03-18 09:59:57.088:[ INFO] [18984] []
[XNIO-2 task-1:107840] [io.seata.tm.api.DefaultGlobalTransaction.rollback:178]
[10.168.2.140:8091:135607899824493054] rollback status: Rollbacked
[example-admin-test1:8781::] 2024-03-18 09:59:57.089:[ WARN] [18984] []
[XNIO-2 task-1:107841]
[c.n.e.c.b.handler.AbstractGlobalExceptionHandler.silenceException:74]
SilenceException:rollback exception
```
### Reason analyze (If you can)
- clue 1:I debug seata's `AbstractUndoLogManager.class`, I noticed that it
query undo log from ds_order_0, not ds_user, so it can't find undo log data, so
**the dataSouceProxy is wrong**.

- clue 2:I try to find out where the dataSourceProxy from, and I find it's
from DataSourceManager, there is a dataSourceCache here.


**I think the size of dataSourceCache should be
three,ds_user、ds_order_0、ds_order_1, but now it only has one**.
- clue 3:I want to know how the dataSourceCache init, so I debug the method
`registerResource()` in seata's `DataSourceManager.class`
It be called three times with three different dataSource, but because of the
wrong jdbcUrl, **these three dataSource figure out same resourceId, so the new
one cover the old one in dataSourceCache**.


So now the question is, why the dataSourceProxy -> jdbcUrl is not same with
dataSourceProxy -> targetDataSource -> url ? Who generate the wrong dataSource?
- clue 4:I follow the debug stack, I find it from
`NewMetaDataContextsFactory.create()` method
```
Map<String, ShardingSphereDatabase> databases = isDatabaseMetaDataExisted
? NewInternalMetaDataFactory.create(persistService,
effectiveDatabaseConfigs, props, instanceContext)
: ExternalMetaDataFactory.create(effectiveDatabaseConfigs,
props, instanceContext);
```
step into the method, the key code is
`StorageUnit.getStorageUnitDataSource()`, the size of `storageNodeDataSources`
is one.
```
private DataSource getStorageUnitDataSource(final Map<StorageNode,
DataSource> storageNodeDataSources, final StorageUnitNodeMapper unitNodeMapper)
{
DataSource dataSource =
storageNodeDataSources.get(unitNodeMapper.getStorageNode());
return new CatalogSwitchableDataSource(dataSource,
unitNodeMapper.getCatalog(), unitNodeMapper.getUrl());
}
```

so **the three CatalogSwitchableDataSource object has the same one
dataSource**.
- clue 5:Now I need to find where the storageNodeDataSources from. It's from
`StorageResourceCreator.createStorageResource()`
```
public static StorageResource createStorageResource(final Map<String,
DataSourcePoolProperties> propsMap) {
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>();
Map<String, StorageUnitNodeMapper> mappers = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry :
propsMap.entrySet()) {
String storageUnitName = entry.getKey();
StorageUnitNodeMapper mapper =
getStorageUnitNodeMapper(storageUnitName, entry.getValue());
mappers.put(storageUnitName, mapper);
// -------- this is the reason --------
storageNodes.computeIfAbsent(mapper.getStorageNode(), key ->
DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true,
storageNodes.values()));
}
return new StorageResource(storageNodes, mappers);
}
```
The storageNode with name "example", it's dataSource is just **same with the
first storageUnit**.
### Steps to reproduce the behavior, such as: SQL to execute, sharding rule
configuration, when exception occur etc.
- sharding.yaml in springboot
```
databaseName: example
mode:
type: Cluster
repository:
type: ZooKeeper
props:
namespace: example
server-lists: 10.168.2.140:2181
maxRetries: 100
retryIntervalMilliseconds: 500
timeToLiveSeconds: 60
operationTimeoutMilliseconds: 30000
transaction:
defaultType: BASE
providerType: Seata
props:
sql-show: true
```
- distsql
```
REGISTER STORAGE UNIT ds_user (
URL="jdbc:mysql://10.168.2.140:3306/ds_user?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true",
USER="example",
PASSWORD="example"
);
REGISTER STORAGE UNIT ds_order_0 (
URL="jdbc:mysql://10.168.2.140:3306/ds_order_0?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true",
USER="example",
PASSWORD="example"
);
REGISTER STORAGE UNIT ds_order_1 (
URL="jdbc:mysql://10.168.2.140:3306/ds_order_1?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true",
USER="example",
PASSWORD="example"
);
CREATE SHARDING TABLE RULE t_order (
DATANODES("ds_order_${0..1}.t_order_${0..1}"),
DATABASE_STRATEGY(TYPE="standard",SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds_order_${user_id
% 2}")))),
TABLE_STRATEGY(TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="t_order_${order_id
% 2}"))))
);
CREATE SHARDING TABLE RULE t_order_item (
DATANODES("ds_order_${0..1}.t_order_item_${0..1}"),
DATABASE_STRATEGY(TYPE="standard",SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="ds_order_${user_id
% 2}")))),
TABLE_STRATEGY(TYPE="standard",SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME="inline",PROPERTIES("algorithm-expression"="t_order_item_${order_id
% 2}"))))
);
LOAD SINGLE TABLE ds_user.t_user;
```
### Example codes for reproduce this issue (such as a github link).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail:
[email protected]
For queries about this service, please contact Infrastructure at:
[email protected]