This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 5d09714550b Improve DriverDataSourceCache use computeIfAbsent avoid
duplicate created. (#20197)
5d09714550b is described below
commit 5d09714550bb6ea448ee9286951ee5812e74deec
Author: wuwen <[email protected]>
AuthorDate: Tue Aug 16 17:48:11 2022 +0800
Improve DriverDataSourceCache use computeIfAbsent avoid duplicate created.
(#20197)
---
.../jdbc/core/driver/DriverDataSourceCache.java | 25 ++++++++--------------
1 file changed, 9 insertions(+), 16 deletions(-)
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
index 550efd58db6..e212b2aac12 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.driver.jdbc.core.driver;
import
org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
-import
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import javax.sql.DataSource;
import java.io.IOException;
@@ -44,23 +43,17 @@ public final class DriverDataSourceCache {
if (dataSourceMap.containsKey(url)) {
return dataSourceMap.get(url);
}
- DataSource dataSource;
+ return dataSourceMap.computeIfAbsent(url,
DriverDataSourceCache::createDataSource);
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T extends Throwable> DataSource createDataSource(final
String url) throws T {
try {
- dataSource =
YamlShardingSphereDataSourceFactory.createDataSource(new
ShardingSphereDriverURL(url).toConfigurationBytes());
+ return YamlShardingSphereDataSourceFactory.createDataSource(new
ShardingSphereDriverURL(url).toConfigurationBytes());
} catch (final IOException ex) {
- throw new SQLException(ex);
- }
- DataSource previousDataSource = dataSourceMap.putIfAbsent(url,
dataSource);
- if (null == previousDataSource) {
- return dataSource;
- }
- try {
- ((ShardingSphereDataSource) dataSource).close();
- // CHECKSTYLE:OFF
- } catch (Exception ex) {
- // CHECKSTYLE:ON
- throw new SQLException(ex);
+ throw (T) new SQLException(ex);
+ } catch (SQLException e) {
+ throw (T) e;
}
- return previousDataSource;
}
}