26
26
import org .springframework .util .StringUtils ;
27
27
28
28
import java .sql .SQLException ;
29
- import java .util .LinkedList ;
30
- import java .util .List ;
31
29
import java .util .Properties ;
32
30
33
31
/**
38
36
@ ConfigurationProperties (prefix = "hsweb.dynamicDatasource" )
39
37
public class DynamicDataSourceProperties
40
38
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 ;
66
59
67
60
public int getTransactionTimeout () {
68
61
return transactionTimeout ;
@@ -207,9 +200,6 @@ public Properties getProperties() {
207
200
if (properties == null ) {
208
201
properties = new Properties ();
209
202
}
210
- properties .put ("username" , getUsername ());
211
- properties .put ("password" , getPassword ());
212
- properties .put ("url" , getUrl ());
213
203
return properties ;
214
204
}
215
205
@@ -228,13 +218,18 @@ public void afterPropertiesSet() throws Exception {
228
218
type = DatabaseType .fromJdbcUrl (getUrl ());
229
219
}
230
220
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 ();
231
225
}
232
226
233
227
public String lookupSupportDatasourceName () throws ClassNotFoundException {
234
- for (String dsClass : supportDatasourceType ) {
228
+ for (DatasourceTypeSupport support : DatasourceTypeSupport . values () ) {
235
229
try {
236
- ClassUtils .forName (dsClass , classLoader );
237
- return dsClass ;
230
+ ClassUtils .forName (support .className , classLoader );
231
+ datasourceTypeSupport = support ;
232
+ return support .className ;
238
233
} catch (ClassNotFoundException e ) {
239
234
}
240
235
}
@@ -265,4 +260,44 @@ public void putProperties(AtomikosDataSourceBean dataSourceBean) {
265
260
}
266
261
}
267
262
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
+ }
268
303
}
0 commit comments