1818package org .apache .shardingsphere .mode .metadata .manager .rule ;
1919
2020import lombok .RequiredArgsConstructor ;
21+ import lombok .SneakyThrows ;
2122import org .apache .shardingsphere .infra .config .rule .RuleConfiguration ;
2223import org .apache .shardingsphere .infra .config .rule .scope .DatabaseRuleConfiguration ;
2324import org .apache .shardingsphere .infra .config .rule .scope .DatabaseRuleConfigurationEmptyChecker ;
@@ -65,9 +66,10 @@ public synchronized void alter(final String databaseName, final RuleConfiguratio
6566 ((PartialRuleUpdateSupported ) toBeChangedRule .get ()).updateConfiguration (ruleConfig );
6667 return ;
6768 }
68- rules .removeIf (each -> each .getConfiguration ().getClass ().isAssignableFrom (ruleConfig .getClass ()));
69+ Collection <ShardingSphereRule > toBeRemovedRules = rules .stream ().filter (each -> each .getConfiguration ().getClass ().isAssignableFrom (ruleConfig .getClass ())).collect (Collectors .toList ());
70+ rules .removeAll (toBeRemovedRules );
6971 rules .add (DatabaseRulesBuilder .build (databaseName , database .getProtocolType (), database .getRuleMetaData ().getRules (), ruleConfig , computeNodeInstanceContext , database .getResourceMetaData ()));
70- refreshMetadata (databaseName , rules );
72+ refreshMetadata (databaseName , rules , toBeRemovedRules );
7173 }
7274
7375 /**
@@ -86,16 +88,27 @@ public synchronized void drop(final String databaseName, final RuleConfiguration
8688 ((PartialRuleUpdateSupported ) toBeChangedRule .get ()).updateConfiguration (ruleConfig );
8789 return ;
8890 }
89- rules .removeIf (each -> each .getConfiguration ().getClass ().isAssignableFrom (ruleConfig .getClass ()));
91+ Collection <ShardingSphereRule > toBeRemovedRules = rules .stream ().filter (each -> each .getConfiguration ().getClass ().isAssignableFrom (ruleConfig .getClass ())).collect (Collectors .toList ());
92+ rules .removeAll (toBeRemovedRules );
9093 if (!TypedSPILoader .getService (DatabaseRuleConfigurationEmptyChecker .class , ruleConfig .getClass ()).isEmpty ((DatabaseRuleConfiguration ) ruleConfig )) {
9194 rules .add (DatabaseRulesBuilder .build (
9295 databaseName , database .getProtocolType (), database .getRuleMetaData ().getRules (), ruleConfig , computeNodeInstanceContext , database .getResourceMetaData ()));
9396 }
94- refreshMetadata (databaseName , rules );
97+ refreshMetadata (databaseName , rules , toBeRemovedRules );
9598 }
9699
97- private void refreshMetadata (final String databaseName , final Collection <ShardingSphereRule > rules ) throws SQLException {
100+ private void refreshMetadata (final String databaseName , final Collection <ShardingSphereRule > rules , final Collection < ShardingSphereRule > toBeRemovedRules ) throws SQLException {
98101 Collection <RuleConfiguration > ruleConfigs = rules .stream ().map (ShardingSphereRule ::getConfiguration ).collect (Collectors .toList ());
99102 metaDataContexts .update (new MetaDataContextsFactory (metaDataPersistFacade , computeNodeInstanceContext ).createByAlterRule (databaseName , false , ruleConfigs , metaDataContexts ));
103+ closeOriginalRules (toBeRemovedRules );
104+ }
105+
106+ @ SneakyThrows (Exception .class )
107+ private void closeOriginalRules (final Collection <ShardingSphereRule > toBeRemovedRules ) {
108+ for (ShardingSphereRule each : toBeRemovedRules ) {
109+ if (each instanceof AutoCloseable ) {
110+ ((AutoCloseable ) each ).close ();
111+ }
112+ }
100113 }
101114}
0 commit comments