jingshanglu commented on code in PR #18911:
URL: https://github.com/apache/shardingsphere/pull/18911#discussion_r916494530
##########
shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java:
##########
@@ -82,23 +88,39 @@ public synchronized void addResource(final
ShardingSphereDatabase database) {
if (null == database) {
return;
}
- ShardingSphereTransactionManagerEngine previousEngine =
resources.put(database.getName(), createTransactionManagerEngine(database));
- if (null != previousEngine) {
- closeEngine(previousEngine);
- }
+ databases.put(database.getName(), database);
+ rebuildEngine();
}
@Override
public synchronized void closeStaleResource(final String databaseName) {
- ShardingSphereTransactionManagerEngine engine =
resources.remove(databaseName);
- if (null != engine) {
- closeEngine(engine);
+ if (!databases.containsKey(databaseName)) {
+ return;
}
+ databases.remove(databaseName);
+ rebuildEngine();
}
@Override
- public void closeStaleResources() {
- resources.values().forEach(this::closeEngine);
+ public synchronized void closeStaleResource() {
+ databases.clear();
+ closeEngine();
+ }
+
+ private void rebuildEngine() {
+ ShardingSphereTransactionManagerEngine previousEngine =
transactionManagerEngine;
+ if (null != previousEngine) {
+ closeEngine(previousEngine);
+ }
+ transactionManagerEngine = createTransactionManagerEngine(databases);
+ }
+
+ private void closeEngine() {
+ ShardingSphereTransactionManagerEngine engine =
transactionManagerEngine;
+ if (null != engine) {
+ transactionManagerEngine = new
ShardingSphereTransactionManagerEngine();
+ closeEngine(engine);
Review Comment:
May need to close and then new.
##########
shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java:
##########
@@ -121,6 +122,8 @@ public void close() throws Exception {
each.close();
}
cachedDataSources.clear();
- xaTransactionManagerProvider.close();
+ if (Objects.nonNull(xaTransactionManagerProvider)) {
+ xaTransactionManagerProvider.close();
+ }
Review Comment:
`xaTransactionManagerProvider` must not be empty, maybe it is ok to close
`xaTransactionManagerProvider` directly .
##########
shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java:
##########
@@ -50,30 +51,35 @@ public final class TransactionRule implements GlobalRule,
ResourceHeldRule<Shard
private final Map<String, ShardingSphereDatabase> databases;
- private final Map<String, ShardingSphereTransactionManagerEngine>
resources;
+ private volatile ShardingSphereTransactionManagerEngine
transactionManagerEngine;
public TransactionRule(final TransactionRuleConfiguration ruleConfig,
final Map<String, ShardingSphereDatabase> databases, final InstanceContext
instanceContext) {
configuration = ruleConfig;
defaultType =
TransactionType.valueOf(ruleConfig.getDefaultType().toUpperCase());
providerType = ruleConfig.getProviderType();
props = ruleConfig.getProps();
this.databases = databases;
- resources = createTransactionManagerEngines(databases,
instanceContext);
+ transactionManagerEngine = createTransactionManagerEngine(databases);
}
- private Map<String, ShardingSphereTransactionManagerEngine>
createTransactionManagerEngines(final Map<String, ShardingSphereDatabase>
databases, final InstanceContext instanceContext) {
- Map<String, ShardingSphereTransactionManagerEngine> result = new
HashMap<>(databases.keySet().size(), 1);
+ private synchronized ShardingSphereTransactionManagerEngine
createTransactionManagerEngine(final Map<String, ShardingSphereDatabase>
databases) {
+ if (databases.size() == 0) {
+ return new ShardingSphereTransactionManagerEngine();
+ }
+ ShardingSphereTransactionManagerEngine result = new
ShardingSphereTransactionManagerEngine();
+ Map<String, DataSource> dataSourceMap = new
HashMap<>(databases.size());
+ DatabaseType databaseType = null;
for (Entry<String, ShardingSphereDatabase> entry :
databases.entrySet()) {
- result.put(entry.getKey(),
createTransactionManagerEngine(entry.getValue()));
+
dataSourceMap.putAll(entry.getValue().getResource().getDataSources());
+ databaseType = entry.getValue().getProtocolType();
}
+ result.init(databaseType, dataSourceMap, providerType);
return result;
}
- private ShardingSphereTransactionManagerEngine
createTransactionManagerEngine(final ShardingSphereDatabase database) {
- ShardingSphereTransactionManagerEngine result = new
ShardingSphereTransactionManagerEngine();
- ShardingSphereResource resource = database.getResource();
- result.init(resource.getDatabaseType(), resource.getDataSources(),
providerType);
- return result;
+ @Override
+ public ShardingSphereTransactionManagerEngine getResource() {
+ return transactionManagerEngine;
Review Comment:
The function may be redundant, `Getter` is enough.
--
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]