Skip to content

Commit 1f67517

Browse files
committed
优化数据源配置逻辑
1 parent c523548 commit 1f67517

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class DynamicDataSourceProperties
5757
private Properties properties = null;
5858
private ClassLoader classLoader = null;
5959
private DatasourceTypeSupport datasourceTypeSupport = null;
60-
private AtomikosProperties icatch = new AtomikosProperties();
60+
private AtomikosProperties icatch = new AtomikosProperties();
6161

6262
public int getTransactionTimeout() {
6363
return transactionTimeout;
@@ -221,7 +221,7 @@ public void setProperties(Properties properties) {
221221
public void afterPropertiesSet() throws Exception {
222222
Assert.notNull(url);
223223
Assert.notNull(username);
224-
if (datasourceName == null) {
224+
if (datasourceName == null || datasourceTypeSupport == null) {
225225
datasourceName = lookupSupportDatasourceName();
226226
}
227227
if (type == null) {
@@ -241,6 +241,7 @@ public String lookupSupportDatasourceName() throws ClassNotFoundException {
241241
datasourceTypeSupport = support;
242242
return support.className;
243243
} catch (ClassNotFoundException e) {
244+
244245
}
245246
}
246247
return getType().getXaDataSourceClassName();
@@ -290,7 +291,7 @@ public void putDefaultProperties(Properties properties) {
290291
properties.putIfAbsent("testOnBorrow", false);
291292
properties.putIfAbsent("testOnReturn", false);
292293
properties.putIfAbsent("poolPreparedStatements", true);
293-
properties.putIfAbsent("maxOpenPreparedStatements", 20);
294+
properties.putIfAbsent("maxOpenPreparedStatements", 300);
294295
}
295296
};
296297

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.hsweb.web.service.datasource.DynamicDataSourceService;
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
31+
import org.springframework.beans.BeanUtils;
3132
import org.springframework.beans.factory.annotation.Autowired;
3233
import org.springframework.stereotype.Service;
3334

@@ -130,15 +131,23 @@ protected CacheInfo getCache(String id) {
130131
}
131132
}
132133

134+
@Autowired
135+
private DynamicDataSourceProperties defaultProperties;
136+
133137
protected javax.sql.DataSource createDataSource(DataSource dataSource) {
134138
DynamicDataSourceProperties properties = new DynamicDataSourceProperties();
139+
try {
140+
BeanUtils.copyProperties(defaultProperties, properties);
141+
} catch (Exception e) {
142+
}
135143
properties.setName("ds_" + dataSource.getId());
136144
properties.setBeanClassLoader(this.getClass().getClassLoader());
137145
properties.setUsername(dataSource.getUsername());
138146
properties.setPassword(dataSource.getPassword());
139147
properties.setUrl(dataSource.getUrl());
140148
properties.setType(DatabaseType.fromJdbcUrl(dataSource.getUrl()));
141149
properties.setTestQuery(dataSource.getTestSql());
150+
142151
Map<String, Object> otherProperties = dataSource.getProperties();
143152
int initTimeout = StringUtils.toInt(otherProperties.getOrDefault("initTimeOut", 30 * 1000));
144153
otherProperties.remove("initTimeOut");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/**
3838
* 动态数据源sql执行器
3939
*/
40+
@Transactional(rollbackFor = Exception.class)
4041
public class DynamicDataSourceSqlExecutorService extends AbstractJdbcSqlExecutor implements ExpressionScopeBean {
4142

4243
@Resource

0 commit comments

Comments
 (0)