16
16
*/
17
17
package org .apache .seata .core .store .db ;
18
18
19
+ import static org .apache .seata .common .DefaultValues .DEFAULT_DB_MAX_CONN ;
20
+ import static org .apache .seata .common .DefaultValues .DEFAULT_DB_MIN_CONN ;
21
+
19
22
import java .io .File ;
20
23
import java .net .MalformedURLException ;
21
24
import java .net .URL ;
24
27
import java .util .Map ;
25
28
import java .util .Objects ;
26
29
import java .util .stream .Stream ;
27
-
28
30
import javax .sql .DataSource ;
29
-
30
31
import org .apache .seata .common .exception .ShouldNeverHappenException ;
31
32
import org .apache .seata .common .exception .StoreException ;
32
33
import org .apache .seata .common .executor .Initialize ;
39
40
import org .slf4j .Logger ;
40
41
import org .slf4j .LoggerFactory ;
41
42
42
- import static org .apache .seata .common .DefaultValues .DEFAULT_DB_MAX_CONN ;
43
- import static org .apache .seata .common .DefaultValues .DEFAULT_DB_MIN_CONN ;
44
-
45
43
/**
46
44
* The abstract datasource provider
47
- *
45
+ *
48
46
*/
49
47
public abstract class AbstractDataSourceProvider implements DataSourceProvider , Initialize {
50
48
@@ -57,18 +55,18 @@ public abstract class AbstractDataSourceProvider implements DataSourceProvider,
57
55
*/
58
56
protected static final Configuration CONFIG = ConfigurationFactory .getInstance ();
59
57
60
- private final static String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver" ;
58
+ private static final String MYSQL_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver" ;
61
59
62
- private final static String MYSQL8_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver" ;
60
+ private static final String MYSQL8_DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver" ;
63
61
64
- private final static String MYSQL_DRIVER_FILE_PREFIX = "mysql-connector-j" ;
62
+ private static final String MYSQL_DRIVER_FILE_PREFIX = "mysql-connector-j" ;
65
63
66
- private final static Map <String , ClassLoader > MYSQL_DRIVER_LOADERS ;
64
+ private static final Map <String , ClassLoader > DRIVER_LOADERS ;
67
65
68
66
private static final long DEFAULT_DB_MAX_WAIT = 5000 ;
69
67
70
68
static {
71
- MYSQL_DRIVER_LOADERS = createMysqlDriverClassLoaders ();
69
+ DRIVER_LOADERS = createMysqlDriverClassLoaders ();
72
70
}
73
71
74
72
@ Override
@@ -87,7 +85,7 @@ public DataSource generate() {
87
85
}
88
86
89
87
public void validate () {
90
- //valid driver class name
88
+ // valid driver class name
91
89
String driverClassName = getDriverClassName ();
92
90
ClassLoader loader = getDriverClassLoader ();
93
91
if (null == loader ) {
@@ -106,14 +104,21 @@ public void validate() {
106
104
.map (file -> file .isFile () ? file .getParentFile () : file )
107
105
.filter (Objects ::nonNull )
108
106
.filter (File ::isDirectory )
109
- .map (file -> new File (file , "jdbc" ))
107
+ // Only the MySQL driver needs to be placed in the jdbc folder.
108
+ .map (file -> (MYSQL8_DRIVER_CLASS_NAME .equals (driverClassName )
109
+ || MYSQL_DRIVER_CLASS_NAME .equals (driverClassName ))
110
+ ? new File (file , "jdbc" )
111
+ : file )
110
112
.filter (File ::exists )
111
113
.filter (File ::isDirectory )
112
- .distinct ().findAny ().orElseThrow (() -> new ShouldNeverHappenException ("can not find jdbc folder" )).getAbsolutePath ();
114
+ .distinct ()
115
+ .findAny ()
116
+ .map (File ::getAbsolutePath )
117
+ .orElseThrow (() -> new ShouldNeverHappenException ("cannot find jdbc folder" ));
113
118
throw new StoreException (String .format (
114
- "The driver {%s} cannot be found in the path %s. Please ensure that the appropriate database driver dependencies are included in the classpath." , driverClassName , driverClassPath ));
119
+ "The driver {%s} cannot be found in the path %s. Please ensure that the appropriate database driver dependencies are included in the classpath." ,
120
+ driverClassName , driverClassPath ));
115
121
}
116
-
117
122
}
118
123
/**
119
124
* generate the datasource
@@ -139,7 +144,7 @@ protected String getDriverClassName() {
139
144
String driverClassName = CONFIG .getConfig (ConfigurationKeys .STORE_DB_DRIVER_CLASS_NAME );
140
145
if (StringUtils .isBlank (driverClassName )) {
141
146
throw new StoreException (
142
- String .format ("the {%s} can't be empty" , ConfigurationKeys .STORE_DB_DRIVER_CLASS_NAME ));
147
+ String .format ("the {%s} can't be empty" , ConfigurationKeys .STORE_DB_DRIVER_CLASS_NAME ));
143
148
}
144
149
return driverClassName ;
145
150
}
@@ -150,12 +155,12 @@ protected String getDriverClassName() {
150
155
* @return the db max wait
151
156
*/
152
157
protected Long getMaxWait () {
153
- Long maxWait = CONFIG .getLong (ConfigurationKeys .STORE_DB_MAX_WAIT , DEFAULT_DB_MAX_WAIT );
154
- return maxWait ;
158
+ return CONFIG .getLong (ConfigurationKeys .STORE_DB_MAX_WAIT , DEFAULT_DB_MAX_WAIT );
155
159
}
156
160
157
161
protected ClassLoader getDriverClassLoader () {
158
- return MYSQL_DRIVER_LOADERS .getOrDefault (getDriverClassName (), ClassLoader .getSystemClassLoader ());
162
+ return DRIVER_LOADERS .getOrDefault (
163
+ getDriverClassName (), this .getClass ().getClassLoader ());
159
164
}
160
165
161
166
private static Map <String , ClassLoader > createMysqlDriverClassLoaders () {
@@ -168,39 +173,39 @@ private static Map<String, ClassLoader> createMysqlDriverClassLoaders() {
168
173
return loaders ;
169
174
}
170
175
Stream .of (cp .split (File .pathSeparator ))
171
- .map (File ::new )
172
- .filter (File ::exists )
173
- .map (file -> file .isFile () ? file .getParentFile () : file )
174
- .filter (Objects ::nonNull )
175
- .filter (File ::isDirectory )
176
- .map (file -> new File (file , "jdbc" ))
177
- .filter (File ::exists )
178
- .filter (File ::isDirectory )
179
- .distinct ()
180
- .flatMap (file -> {
181
- File [] files = file .listFiles ((f , name ) -> name .startsWith (MYSQL_DRIVER_FILE_PREFIX ));
182
- if (files != null ) {
183
- return Stream .of (files );
184
- } else {
185
- return Stream .of ();
186
- }
187
- })
188
- .forEach (file -> {
189
- if (loaders .containsKey (MYSQL8_DRIVER_CLASS_NAME ) && loaders .containsKey (MYSQL_DRIVER_CLASS_NAME )) {
190
- return ;
191
- }
192
- try {
193
- URL url = file .toURI ().toURL ();
194
- ClassLoader loader = new URLClassLoader (new URL []{url }, ClassLoader .getSystemClassLoader ());
176
+ .map (File ::new )
177
+ .filter (File ::exists )
178
+ .map (file -> file .isFile () ? file .getParentFile () : file )
179
+ .filter (Objects ::nonNull )
180
+ .filter (File ::isDirectory )
181
+ .map (file -> new File (file , "jdbc" ))
182
+ .filter (File ::exists )
183
+ .filter (File ::isDirectory )
184
+ .distinct ()
185
+ .flatMap (file -> {
186
+ File [] files = file .listFiles ((f , name ) -> name .startsWith (MYSQL_DRIVER_FILE_PREFIX ));
187
+ if (files != null ) {
188
+ return Stream .of (files );
189
+ } else {
190
+ return Stream .of ();
191
+ }
192
+ })
193
+ .forEach (file -> {
194
+ if (loaders .containsKey (MYSQL8_DRIVER_CLASS_NAME ) && loaders .containsKey (MYSQL_DRIVER_CLASS_NAME )) {
195
+ return ;
196
+ }
195
197
try {
196
- loader .loadClass (MYSQL8_DRIVER_CLASS_NAME );
197
- loaders .putIfAbsent (MYSQL8_DRIVER_CLASS_NAME , loader );
198
- } catch (ClassNotFoundException e ) {
199
- loaders .putIfAbsent (MYSQL_DRIVER_CLASS_NAME , loader );
198
+ URL url = file .toURI ().toURL ();
199
+ ClassLoader loader = new URLClassLoader (new URL [] {url }, ClassLoader .getSystemClassLoader ());
200
+ try {
201
+ loader .loadClass (MYSQL8_DRIVER_CLASS_NAME );
202
+ loaders .putIfAbsent (MYSQL8_DRIVER_CLASS_NAME , loader );
203
+ } catch (ClassNotFoundException e ) {
204
+ loaders .putIfAbsent (MYSQL_DRIVER_CLASS_NAME , loader );
205
+ }
206
+ } catch (MalformedURLException ignore ) {
200
207
}
201
- } catch (MalformedURLException ignore ) {
202
- }
203
- });
208
+ });
204
209
return loaders ;
205
210
}
206
211
@@ -243,8 +248,8 @@ protected String getPassword() {
243
248
password = ConfigTools .publicDecrypt (password , publicKey );
244
249
} catch (Exception e ) {
245
250
LOGGER .error (
246
- "decryption failed,please confirm whether the ciphertext and secret key are correct! error msg: {}" ,
247
- e .getMessage ());
251
+ "decryption failed,please confirm whether the ciphertext and secret key are correct! error msg: {}" ,
252
+ e .getMessage ());
248
253
}
249
254
}
250
255
return password ;
@@ -292,5 +297,4 @@ protected String getValidationQuery(DBType dbType) {
292
297
protected String getPublicKey () {
293
298
return CONFIG .getConfig (ConfigurationKeys .STORE_PUBLIC_KEY );
294
299
}
295
-
296
300
}
0 commit comments