Skip to content

Commit ade0a7f

Browse files
committed
优化动态数据源,添加默认配置.
1 parent be412bb commit ade0a7f

File tree

3 files changed

+78
-49
lines changed

3 files changed

+78
-49
lines changed

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import com.atomikos.icatch.jta.UserTransactionImp;
2020
import com.atomikos.icatch.jta.UserTransactionManager;
2121
import com.atomikos.jdbc.AtomikosDataSourceBean;
22-
import org.hsweb.web.core.datasource.DynamicDataSource;
2322
import org.hsweb.web.core.datasource.DataSourceHolder;
23+
import org.hsweb.web.core.datasource.DynamicDataSource;
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.beans.factory.annotation.Qualifier;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2727
import org.springframework.boot.context.properties.EnableConfigurationProperties;
28+
import org.springframework.cache.annotation.Cacheable;
2829
import org.springframework.context.annotation.Bean;
2930
import org.springframework.context.annotation.ComponentScan;
3031
import org.springframework.context.annotation.Configuration;
@@ -48,6 +49,8 @@ public class DynamicDataSourceAutoConfiguration {
4849
*/
4950
@Primary
5051
@Bean(initMethod = "init", name = "dataSource", destroyMethod = "close")
52+
@ConditionalOnMissingBean(DataSource.class)
53+
@Cacheable
5154
public DataSource dataSource() {
5255
AtomikosDataSourceBean dataSourceBean = new AtomikosDataSourceBean();
5356
properties.putProperties(dataSourceBean);

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceProperties.java

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.springframework.util.StringUtils;
2727

2828
import java.sql.SQLException;
29-
import java.util.LinkedList;
30-
import java.util.List;
3129
import java.util.Properties;
3230

3331
/**
@@ -38,31 +36,26 @@
3836
@ConfigurationProperties(prefix = "hsweb.dynamicDatasource")
3937
public class DynamicDataSourceProperties
4038
implements BeanClassLoaderAware, InitializingBean {
41-
private static final List<String> supportDatasourceType;
42-
private String name = "core";
43-
private DatabaseType type = null;
44-
private String datasourceName = null;
45-
private String username = "sa";
46-
private String password = "";
47-
private String url = "jdbc:h2:file:./data/h2db;DB_CLOSE_ON_EXIT=FALSE";
48-
private String testQuery;
49-
private int loginTimeout = 0;
50-
private int maxLifetime = 0;
51-
private int minPoolSize = 2;
52-
private int maxPoolSize = 20;
53-
private int borrowConnectionTimeout = 30;
54-
private int reapTimeout = 0;
55-
private int maxIdleTime = 60;
56-
private int maintenanceInterval = 60;
57-
private int defaultIsolationLevel = -1;
58-
private int transactionTimeout = 300;
59-
private Properties properties = null;
60-
private ClassLoader classLoader = null;
61-
62-
static {
63-
supportDatasourceType = new LinkedList<>();
64-
supportDatasourceType.add("com.alibaba.druid.pool.xa.DruidXADataSource");
65-
}
39+
private String name = "core";
40+
private DatabaseType type = null;
41+
private String datasourceName = null;
42+
private String username = "sa";
43+
private String password = "";
44+
private String url = "jdbc:h2:file:./data/h2db;DB_CLOSE_ON_EXIT=FALSE";
45+
private String testQuery = null;
46+
private int loginTimeout = 0;
47+
private int maxLifetime = 0;
48+
private int minPoolSize = 3;
49+
private int maxPoolSize = 80;
50+
private int borrowConnectionTimeout = 60;
51+
private int reapTimeout = 0;
52+
private int maxIdleTime = 60;
53+
private int maintenanceInterval = 60;
54+
private int defaultIsolationLevel = -1;
55+
private int transactionTimeout = 300;
56+
private Properties properties = null;
57+
private ClassLoader classLoader = null;
58+
private DatasourceTypeSupport datasourceTypeSupport = null;
6659

6760
public int getTransactionTimeout() {
6861
return transactionTimeout;
@@ -207,9 +200,6 @@ public Properties getProperties() {
207200
if (properties == null) {
208201
properties = new Properties();
209202
}
210-
properties.put("username", getUsername());
211-
properties.put("password", getPassword());
212-
properties.put("url", getUrl());
213203
return properties;
214204
}
215205

@@ -228,13 +218,18 @@ public void afterPropertiesSet() throws Exception {
228218
type = DatabaseType.fromJdbcUrl(getUrl());
229219
}
230220
if (!StringUtils.hasText(testQuery)) testQuery = getType().getTestQuery();
221+
getProperties().put(datasourceTypeSupport.usernameProperty, getUsername());
222+
getProperties().put(datasourceTypeSupport.passwordProperty, getPassword());
223+
getProperties().put(datasourceTypeSupport.urlProperty, getUrl());
224+
initDefaultProperties();
231225
}
232226

233227
public String lookupSupportDatasourceName() throws ClassNotFoundException {
234-
for (String dsClass : supportDatasourceType) {
228+
for (DatasourceTypeSupport support : DatasourceTypeSupport.values()) {
235229
try {
236-
ClassUtils.forName(dsClass, classLoader);
237-
return dsClass;
230+
ClassUtils.forName(support.className, classLoader);
231+
datasourceTypeSupport = support;
232+
return support.className;
238233
} catch (ClassNotFoundException e) {
239234
}
240235
}
@@ -265,4 +260,44 @@ public void putProperties(AtomikosDataSourceBean dataSourceBean) {
265260
}
266261
}
267262

263+
public void initDefaultProperties() {
264+
datasourceTypeSupport.putDefaultProperties(getProperties());
265+
}
266+
267+
private enum DatasourceTypeSupport {
268+
druid("com.alibaba.druid.pool.xa.DruidXADataSource", "username", "password", "url") {
269+
@Override
270+
public void putDefaultProperties(Properties properties) {
271+
super.putDefaultProperties(properties);
272+
properties.putIfAbsent("filters", "stat");
273+
properties.putIfAbsent("maxActive", 200);
274+
properties.putIfAbsent("initialSize", 3);
275+
properties.putIfAbsent("minIdle", 3);
276+
properties.putIfAbsent("maxWait", 5000);
277+
properties.putIfAbsent("timeBetweenEvictionRunsMillis", 60000);
278+
properties.putIfAbsent("minEvictableIdleTimeMillis", 1800000);
279+
properties.putIfAbsent("testWhileIdle", true);
280+
properties.putIfAbsent("testOnBorrow", false);
281+
properties.putIfAbsent("testOnReturn", false);
282+
properties.putIfAbsent("poolPreparedStatements", true);
283+
properties.putIfAbsent("maxOpenPreparedStatements", 20);
284+
}
285+
};
286+
287+
DatasourceTypeSupport(String className, String usernameProperty, String passwordProperty, String urlProperty) {
288+
this.className = className;
289+
this.usernameProperty = usernameProperty;
290+
this.passwordProperty = passwordProperty;
291+
this.urlProperty = urlProperty;
292+
}
293+
294+
final String className;
295+
final String usernameProperty;
296+
final String passwordProperty;
297+
final String urlProperty;
298+
299+
public void putDefaultProperties(Properties properties) {
300+
301+
}
302+
}
268303
}

hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceServiceImpl.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import com.atomikos.jdbc.AtomikosDataSourceBean;
2020
import com.atomikos.jdbc.AtomikosSQLException;
21-
import org.apache.commons.beanutils.BeanUtilsBean;
22-
import org.apache.commons.beanutils.PropertyUtilsBean;
2321
import org.hsweb.concurrent.lock.LockFactory;
2422
import org.hsweb.web.bean.po.datasource.DataSource;
2523
import org.hsweb.web.core.datasource.DatabaseType;
@@ -30,18 +28,14 @@
3028
import org.slf4j.Logger;
3129
import org.slf4j.LoggerFactory;
3230
import org.springframework.beans.factory.annotation.Autowired;
33-
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
34-
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
3531
import org.springframework.stereotype.Service;
3632

3733
import javax.annotation.PostConstruct;
3834
import javax.annotation.PreDestroy;
3935
import javax.annotation.Resource;
4036
import java.io.Closeable;
4137
import java.io.IOException;
42-
import java.lang.reflect.InvocationTargetException;
4338
import java.util.Map;
44-
import java.util.Properties;
4539
import java.util.concurrent.ConcurrentHashMap;
4640
import java.util.concurrent.ConcurrentMap;
4741
import java.util.concurrent.locks.ReadWriteLock;
@@ -144,22 +138,18 @@ protected javax.sql.DataSource createDataSource(DataSource dataSource) {
144138
properties.setUrl(dataSource.getUrl());
145139
properties.setType(DatabaseType.fromJdbcUrl(dataSource.getUrl()));
146140
properties.setTestQuery(dataSource.getTestSql());
141+
Map<String, Object> otherProperties = dataSource.getProperties();
147142
try {
148143
properties.afterPropertiesSet();
149144
} catch (Exception e) {
150145
throw new RuntimeException(e);
151146
}
152-
Map<String, Object> otherProperties = dataSource.getProperties();
153147
if (otherProperties != null) {
154-
PropertyUtilsBean propertyUtilsBean = BeanUtilsBean.getInstance().getPropertyUtils();
155-
otherProperties.forEach((k, v) -> {
156-
try {
157-
propertyUtilsBean.setProperty(properties, k, v);
158-
} catch (Exception e) {
159-
logger.warn("设置动态数据源配置失败", e);
160-
}
161-
});
148+
properties.getProperties().putAll(otherProperties);
149+
} else {
150+
properties.initDefaultProperties();
162151
}
152+
163153
AtomikosDataSourceBean dataSourceBean = new AtomikosDataSourceBean();
164154
properties.putProperties(dataSourceBean);
165155
boolean[] success = new boolean[1];
@@ -169,6 +159,7 @@ protected javax.sql.DataSource createDataSource(DataSource dataSource) {
169159
dataSourceBean.init();
170160
success[0] = true;
171161
} catch (AtomikosSQLException e) {
162+
logger.error("创建数据源失败", e);
172163
closeDataSource(dataSourceBean);
173164
cache.remove(dataSource.getId());
174165
}

0 commit comments

Comments
 (0)