Skip to content

Commit 2c64468

Browse files
Merge pull request #4515 from GDLMadushanka/fixReflection
Fix correlation logs changing the task behaviour
2 parents c32a20a + 5e166d1 commit 2c64468

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

components/org.wso2.micro.integrator.ndatasource.rdbms/src/main/java/org/wso2/micro/integrator/ndatasource/rdbms/CorrelationLogInterceptor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.logging.Log;
3333
import org.apache.commons.logging.LogFactory;
3434
import org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport;
35+
import org.wso2.micro.integrator.ndatasource.rdbms.utils.RDBMSDataSourceUtils;
3536

3637
/**
3738
* Time-Logging interceptor for JDBC pool.
@@ -199,7 +200,13 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
199200
Object result = null;
200201

201202
if (this.delegate != null) {
202-
result = method.invoke(this.delegate, args);
203+
try {
204+
result = method.invoke(this.delegate, args);
205+
} catch (Throwable ex) {
206+
// Unwrap the exceptions related to reflection calls.
207+
ex = RDBMSDataSourceUtils.unwrap(ex);
208+
throw ex;
209+
}
203210
}
204211

205212
//If the query is an execute type of query the time taken is calculated and logged

components/org.wso2.micro.integrator.ndatasource.rdbms/src/main/java/org/wso2/micro/integrator/ndatasource/rdbms/utils/RDBMSDataSourceUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
*/
1818
package org.wso2.micro.integrator.ndatasource.rdbms.utils;
1919

20+
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
23+
import java.lang.reflect.UndeclaredThrowableException;
2224
import java.sql.Connection;
2325
import java.util.Arrays;
2426
import java.util.HashMap;
@@ -374,5 +376,20 @@ private static void handleExternalDataSource(PoolProperties poolProps, RDBMSConf
374376
e.getMessage(), e);
375377
}
376378
}
379+
380+
/**
381+
* Recursively unwraps common reflective wrapper exceptions to reveal the original cause.
382+
* This method traverses through {@link InvocationTargetException} and
383+
* {@link UndeclaredThrowableException} instances until the root cause is found.
384+
*
385+
* @param e the throwable to unwrap
386+
* @return the root cause if {@code e} is wrapped, or {@code e} itself if not
387+
*/
388+
public static Throwable unwrap(Throwable e) {
389+
if (e instanceof InvocationTargetException || e instanceof UndeclaredThrowableException) {
390+
return unwrap(e.getCause());
391+
}
392+
return e;
393+
}
377394

378395
}

0 commit comments

Comments
 (0)