简体   繁体   中英

Using Reflection to invoke method with a WebSphere database connection as argument

I'm trying the use reflection to invoke a method that has a java.sql.Connection as argument.

public void setAndValidateSessionUUID(java.lang.String, java.sql.Connection);

I am on a Websphere 7 context using a jndi path to retrieve the data source and it's connection.

private java.sql.Connection connection;

Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup(this.DataSourceJNDIPath);
this.connection = dataSource.getConnection();

I have the following piece of code to retrieve the method using reflection

public static Method getMethod(Class<?> clazz, String methodName, Class<?>... args) throws SecurityException, NoSuchMethodException {
    return clazz.getMethod(methodName, args);
}

But when I try to retrieve the method it gives me the following error:

java.lang.NoSuchMethodException: setAndValidateSessionUUID(java.lang.String, com.ibm.ws.rsadapter.jdbc.WSJdbcConnection)

I have no problem executing the method without reflection but using it I can't retrieve the method.

Any ideas?

Yes. You may want to read my blog post here but basically, you need to iterate all of the classes methods for (Method method : cls.getMethods()) until you find one where each (and every) method parameter isAssignableFrom your input parameters...

if (!mTypes[i].isAssignableFrom(parameters[i]
        .getClass()))

If it is, store it with toInvoke = method . Then use

toInvoke.invoke(receiver, parameters);

The server returns proxy objects, which you can observe via dataSource.getClass(). On WAS 8.0 and later, you can use the java.sql.Wrapper APIs to call vendor-specific APIs, but on WAS 7.0 and later, you'll need to use WSCallHelper .jdbcCall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM