Skip to content

Commit 8d89189

Browse files
authored
Merge pull request #41 from ctripcorp/ForceSwitchableDataSourceCT
Force switchable data source ct
2 parents 78d7ad8 + 9b5ee84 commit 8d89189

20 files changed

+811
-58
lines changed

dal-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.ctrip.platform</groupId>
66
<artifactId>dal-client</artifactId>
7-
<version>1.17.5</version>
7+
<version>1.17.6</version>
88
<properties>
99
<ver.version>${project.version}</ver.version>
1010
<javax-persistence-version>1.0.2</javax-persistence-version>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.ctrip.platform.dal.common.enums;
2+
3+
/**
4+
* Created by taochen on 2019/8/21.
5+
*/
6+
public enum ForceSwitchedStatus {
7+
UnForceSwitched, ForceSwitching, ForceSwitched
8+
}

dal-client/src/main/java/com/ctrip/platform/dal/dao/configure/ConnectionStringParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
import com.ctrip.platform.dal.dao.helper.ConnectionStringKeyHelper;
5+
import org.apache.commons.lang.StringUtils;
6+
57
import java.util.regex.Matcher;
68
import java.util.regex.Pattern;
79

@@ -140,6 +142,9 @@ public static String replaceHostAndPort(String url, String newHost, String newPo
140142
}
141143

142144
public static HostAndPort parseHostPortFromURL(String url) {
145+
if (StringUtils.isEmpty(url)) {
146+
return new HostAndPort();
147+
}
143148
if (url.toLowerCase().startsWith(MYSQL_URL_PREFIX)) {
144149
Matcher matcher = ipPortPatternInMySQLURL.matcher(url);
145150
if (matcher.find())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ctrip.platform.dal.dao.configure;
2+
3+
/**
4+
* Created by taochen on 2019/8/22.
5+
*/
6+
public interface FirstAidKit {
7+
}

dal-client/src/main/java/com/ctrip/platform/dal/dao/configure/IDataSourceConfigureProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public interface IDataSourceConfigureProvider {
44
// get datasource configure from local cache
5-
IDataSourceConfigure getDataSourceConfigure();
5+
IDataSourceConfigure getDataSourceConfigure() throws Exception;
66

77
// load datasource configure from configure center
88
IDataSourceConfigure forceLoadDataSourceConfigure();
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
package com.ctrip.platform.dal.dao.configure;
2+
3+
/**
4+
* Created by taochen on 2019/8/22.
5+
*/
6+
public class SerializableDataSourceConfig implements IDataSourceConfigure, FirstAidKit {
7+
private String userName;
8+
9+
private String password;
10+
11+
private String connectionUrl;
12+
13+
private String driverClass;
14+
15+
private Boolean testWhileIdle;
16+
17+
private Boolean testOnBorrow;
18+
19+
private Boolean testOnReturn;
20+
21+
private String validationQuery;
22+
23+
private Integer validationQueryTimeout;
24+
25+
private Long validationInterval;
26+
27+
private Integer minEvictableIdleTimeMillis;
28+
29+
private Integer timeBetweenEvictionRunsMillis;
30+
31+
private Integer maxAge;
32+
33+
private Integer maxActive;
34+
35+
private Integer minIdle;
36+
37+
private Integer maxWait;
38+
39+
private Integer initialSize;
40+
41+
private Integer removeAbandonedTimeout;
42+
43+
private Boolean removeAbandoned;
44+
45+
private Boolean logAbandoned;
46+
47+
private String connectionProperties;
48+
49+
private String validatorClassName;
50+
51+
private String initSQL;
52+
53+
private String jdbcInterceptors;
54+
55+
@Override
56+
public String getUserName() {
57+
return userName;
58+
}
59+
60+
@Override
61+
public String getPassword() {
62+
return password;
63+
}
64+
65+
@Override
66+
public String getConnectionUrl() {
67+
return connectionUrl;
68+
}
69+
70+
@Override
71+
public String getDriverClass() {
72+
return driverClass;
73+
}
74+
75+
@Override
76+
public Boolean getTestWhileIdle() {
77+
return testWhileIdle;
78+
}
79+
80+
@Override
81+
public Boolean getTestOnBorrow() {
82+
return testOnBorrow;
83+
}
84+
85+
@Override
86+
public Boolean getTestOnReturn() {
87+
return testOnReturn;
88+
}
89+
90+
@Override
91+
public String getValidationQuery() {
92+
return validationQuery;
93+
}
94+
95+
@Override
96+
public Integer getValidationQueryTimeout() {
97+
return validationQueryTimeout;
98+
}
99+
100+
@Override
101+
public Long getValidationInterval() {
102+
return validationInterval;
103+
}
104+
105+
@Override
106+
public Integer getTimeBetweenEvictionRunsMillis() {
107+
return timeBetweenEvictionRunsMillis;
108+
}
109+
110+
@Override
111+
public Integer getMinEvictableIdleTimeMillis() {
112+
return minEvictableIdleTimeMillis;
113+
}
114+
115+
@Override
116+
public Integer getMaxAge() {
117+
return maxAge;
118+
}
119+
120+
@Override
121+
public Integer getMaxActive() {
122+
return maxActive;
123+
}
124+
125+
@Override
126+
public Integer getMinIdle() {
127+
return minIdle;
128+
}
129+
130+
@Override
131+
public Integer getMaxWait() {
132+
return maxWait;
133+
}
134+
135+
@Override
136+
public Integer getInitialSize() {
137+
return initialSize;
138+
}
139+
140+
@Override
141+
public Integer getRemoveAbandonedTimeout() {
142+
return removeAbandonedTimeout;
143+
}
144+
145+
@Override
146+
public Boolean getRemoveAbandoned() {
147+
return removeAbandoned;
148+
}
149+
150+
@Override
151+
public Boolean getLogAbandoned() {
152+
return logAbandoned;
153+
}
154+
155+
@Override
156+
public String getConnectionProperties() {
157+
return connectionProperties;
158+
}
159+
160+
@Override
161+
public String getValidatorClassName() {
162+
return validatorClassName;
163+
}
164+
165+
@Override
166+
public String getInitSQL() {
167+
return initSQL;
168+
}
169+
170+
@Override
171+
public String getJdbcInterceptors() {
172+
return jdbcInterceptors;
173+
}
174+
175+
public void setUserName(String userName) {
176+
this.userName = userName;
177+
}
178+
179+
public void setPassword(String password) {
180+
this.password = password;
181+
}
182+
183+
public void setConnectionUrl(String connectionUrl) {
184+
this.connectionUrl = connectionUrl;
185+
}
186+
187+
public void setDriverClass(String driverClass) {
188+
this.driverClass = driverClass;
189+
}
190+
191+
public void setTestWhileIdle(Boolean testWhileIdle) {
192+
this.testWhileIdle = testWhileIdle;
193+
}
194+
195+
public void setTestOnBorrow(Boolean testOnBorrow) {
196+
this.testOnBorrow = testOnBorrow;
197+
}
198+
199+
public void setTestOnReturn(Boolean testOnReturn) {
200+
this.testOnReturn = testOnReturn;
201+
}
202+
203+
public void setValidationQuery(String validationQuery) {
204+
this.validationQuery = validationQuery;
205+
}
206+
207+
public void setValidationQueryTimeout(Integer validationQueryTimeout) {
208+
this.validationQueryTimeout = validationQueryTimeout;
209+
}
210+
211+
public void setValidationInterval(Long validationInterval) {
212+
this.validationInterval = validationInterval;
213+
}
214+
215+
public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {
216+
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
217+
}
218+
219+
public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) {
220+
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
221+
}
222+
223+
public void setMaxAge(Integer maxAge) {
224+
this.maxAge = maxAge;
225+
}
226+
227+
public void setMaxActive(Integer maxActive) {
228+
this.maxActive = maxActive;
229+
}
230+
231+
public void setMinIdle(Integer minIdle) {
232+
this.minIdle = minIdle;
233+
}
234+
235+
public void setMaxWait(Integer maxWait) {
236+
this.maxWait = maxWait;
237+
}
238+
239+
public void setInitialSize(Integer initialSize) {
240+
this.initialSize = initialSize;
241+
}
242+
243+
public void setRemoveAbandonedTimeout(Integer removeAbandonedTimeout) {
244+
this.removeAbandonedTimeout = removeAbandonedTimeout;
245+
}
246+
247+
public void setRemoveAbandoned(Boolean removeAbandoned) {
248+
this.removeAbandoned = removeAbandoned;
249+
}
250+
251+
public void setLogAbandoned(Boolean logAbandoned) {
252+
this.logAbandoned = logAbandoned;
253+
}
254+
255+
public void setConnectionProperties(String connectionProperties) {
256+
this.connectionProperties = connectionProperties;
257+
}
258+
259+
public void setValidatorClassName(String validatorClassName) {
260+
this.validatorClassName = validatorClassName;
261+
}
262+
263+
public void setInitSQL(String initSQL) {
264+
this.initSQL = initSQL;
265+
}
266+
267+
public void setJdbcInterceptors(String jdbcInterceptors) {
268+
this.jdbcInterceptors = jdbcInterceptors;
269+
}
270+
271+
public static SerializableDataSourceConfig valueOf(DataSourceConfigure dataSourceConfigure) {
272+
if (dataSourceConfigure == null) {
273+
return null;
274+
}
275+
SerializableDataSourceConfig serializableDataSourceConfig = new SerializableDataSourceConfig();
276+
serializableDataSourceConfig.setUserName(dataSourceConfigure.getUserName());
277+
serializableDataSourceConfig.setPassword(dataSourceConfigure.getPassword());
278+
serializableDataSourceConfig.setConnectionUrl(dataSourceConfigure.getConnectionUrl());
279+
serializableDataSourceConfig.setDriverClass(dataSourceConfigure.getDriverClass());
280+
serializableDataSourceConfig.setTestOnBorrow(dataSourceConfigure.getTestOnBorrow());
281+
serializableDataSourceConfig.setTestOnReturn(dataSourceConfigure.getTestOnReturn());
282+
serializableDataSourceConfig.setTestWhileIdle(dataSourceConfigure.getTestWhileIdle());
283+
serializableDataSourceConfig.setValidationQuery(dataSourceConfigure.getValidationQuery());
284+
serializableDataSourceConfig.setValidationQueryTimeout(dataSourceConfigure.getValidationQueryTimeout());
285+
serializableDataSourceConfig.setValidationInterval(dataSourceConfigure.getValidationInterval());
286+
serializableDataSourceConfig.setValidatorClassName(dataSourceConfigure.getValidatorClassName());
287+
serializableDataSourceConfig.setMinEvictableIdleTimeMillis(dataSourceConfigure.getMinEvictableIdleTimeMillis());
288+
serializableDataSourceConfig.setTimeBetweenEvictionRunsMillis(dataSourceConfigure.getTimeBetweenEvictionRunsMillis());
289+
serializableDataSourceConfig.setMaxAge(dataSourceConfigure.getMaxAge());
290+
serializableDataSourceConfig.setMaxActive(dataSourceConfigure.getMaxActive());
291+
serializableDataSourceConfig.setMinIdle(dataSourceConfigure.getMinIdle());
292+
serializableDataSourceConfig.setMaxWait(dataSourceConfigure.getMaxWait());
293+
serializableDataSourceConfig.setInitialSize(dataSourceConfigure.getInitialSize());
294+
serializableDataSourceConfig.setRemoveAbandonedTimeout(dataSourceConfigure.getRemoveAbandonedTimeout());
295+
serializableDataSourceConfig.setRemoveAbandoned(dataSourceConfigure.getRemoveAbandoned());
296+
serializableDataSourceConfig.setLogAbandoned(dataSourceConfigure.getLogAbandoned());
297+
serializableDataSourceConfig.setConnectionProperties(dataSourceConfigure.getConnectionProperties());
298+
serializableDataSourceConfig.setInitSQL(dataSourceConfigure.getInitSQL());
299+
serializableDataSourceConfig.setJdbcInterceptors(dataSourceConfigure.getJdbcInterceptors());
300+
return serializableDataSourceConfig;
301+
}
302+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ctrip.platform.dal.dao.datasource;
2+
3+
import com.ctrip.platform.dal.dao.configure.DataSourceConfigure;
4+
import com.ctrip.platform.dal.dao.helper.Ordered;
5+
6+
/**
7+
* Created by taochen on 2019/8/22.
8+
*/
9+
public interface DataSourceConfigureConvert extends Ordered {
10+
DataSourceConfigure desEncrypt(DataSourceConfigure dataSourceConfigure);
11+
12+
DataSourceConfigure desDecrypt(DataSourceConfigure dataSourceConfigure);
13+
}

dal-client/src/main/java/com/ctrip/platform/dal/dao/datasource/DataSourceCreator.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99

1010
public class DataSourceCreator {
1111
private static volatile DataSourceCreator creator = null;
12-
private ScheduledExecutorService service =
13-
Executors.newScheduledThreadPool(POOL_SIZE, new CustomThreadFactory(THREAD_NAME));
14-
15-
private static final int INIT_DELAY = 0;
16-
private static final int POOL_SIZE = 1;
17-
private static final String THREAD_NAME = "DataSourceCreator";
1812

1913
private DataSourceCreateTaskFactory factory = null;
2014

@@ -40,7 +34,7 @@ public SingleDataSource createSingleDataSource(String name, DataSourceConfigure
4034
else
4135
task = new DefaultDataSourceCreateTask(name, configure, singleDataSource);
4236
singleDataSource.setTask(task);
43-
service.schedule(task, INIT_DELAY, TimeUnit.MILLISECONDS);
37+
4438
return singleDataSource;
4539
}
4640
}

0 commit comments

Comments
 (0)