16
16
17
17
package org .springframework .jdbc .datasource ;
18
18
19
+ import java .lang .reflect .InvocationTargetException ;
19
20
import java .lang .reflect .Method ;
20
21
import java .sql .Connection ;
21
22
import java .sql .SQLException ;
@@ -141,7 +142,7 @@ protected Connection doGetConnection(@Nullable String username, @Nullable String
141
142
getTargetDataSource () + "], using ConnectionSpec [" + connSpec + "]" );
142
143
}
143
144
// Create Connection through invoking WSDataSource.getConnection(JDBCConnectionSpec)
144
- Connection con = (Connection ) ReflectionUtils . invokeJdbcMethod (
145
+ Connection con = (Connection ) invokeJdbcMethod (
145
146
this .wsDataSourceGetConnectionMethod , obtainTargetDataSource (), connSpec );
146
147
Assert .state (con != null , "No Connection" );
147
148
return con ;
@@ -163,21 +164,40 @@ protected Connection doGetConnection(@Nullable String username, @Nullable String
163
164
protected Object createConnectionSpec (@ Nullable Integer isolationLevel , @ Nullable Boolean readOnlyFlag ,
164
165
@ Nullable String username , @ Nullable String password ) throws SQLException {
165
166
166
- Object connSpec = ReflectionUtils . invokeJdbcMethod (this .newJdbcConnSpecMethod , null );
167
+ Object connSpec = invokeJdbcMethod (this .newJdbcConnSpecMethod , null );
167
168
Assert .state (connSpec != null , "No JDBCConnectionSpec" );
168
169
if (isolationLevel != null ) {
169
- ReflectionUtils . invokeJdbcMethod (this .setTransactionIsolationMethod , connSpec , isolationLevel );
170
+ invokeJdbcMethod (this .setTransactionIsolationMethod , connSpec , isolationLevel );
170
171
}
171
172
if (readOnlyFlag != null ) {
172
- ReflectionUtils . invokeJdbcMethod (this .setReadOnlyMethod , connSpec , readOnlyFlag );
173
+ invokeJdbcMethod (this .setReadOnlyMethod , connSpec , readOnlyFlag );
173
174
}
174
175
// If the username is empty, we'll simply let the target DataSource
175
176
// use its default credentials.
176
177
if (StringUtils .hasLength (username )) {
177
- ReflectionUtils . invokeJdbcMethod (this .setUserNameMethod , connSpec , username );
178
- ReflectionUtils . invokeJdbcMethod (this .setPasswordMethod , connSpec , password );
178
+ invokeJdbcMethod (this .setUserNameMethod , connSpec , username );
179
+ invokeJdbcMethod (this .setPasswordMethod , connSpec , password );
179
180
}
180
181
return connSpec ;
181
182
}
182
183
184
+
185
+ @ Nullable
186
+ private static Object invokeJdbcMethod (Method method , @ Nullable Object target , @ Nullable Object ... args )
187
+ throws SQLException {
188
+ try {
189
+ return method .invoke (target , args );
190
+ }
191
+ catch (IllegalAccessException ex ) {
192
+ ReflectionUtils .handleReflectionException (ex );
193
+ }
194
+ catch (InvocationTargetException ex ) {
195
+ if (ex .getTargetException () instanceof SQLException ) {
196
+ throw (SQLException ) ex .getTargetException ();
197
+ }
198
+ ReflectionUtils .handleInvocationTargetException (ex );
199
+ }
200
+ throw new IllegalStateException ("Should never get here" );
201
+ }
202
+
183
203
}
0 commit comments