0

I'm sure I'm going about this in completely the wrong way, but can someone point out the error in the code below...

MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer (null).get (0); ObjectName mBean = new ObjectName ("Catalina:type=DataSource,path=/<context>,host=localhost,class=javax.sql.DataSource,name=\"<name>\""); String [] params = {"<username>", "<password>"}; Connection myConnection = (Connection) server.invoke (mBean, "getConnection", params, null); Statement myStatement = myConnection.createStatement (); String myResult = myStatement.executeQuery ("SELECT 1 FROM DUAL;").toString (); myConnection.close (); 

The problem is occurring when I try to instantiate the Connection object by invoking the getConnection method on my MBean. I receive the following error...

Aug 6, 2012 8:46:03 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet jsp threw exception java.lang.IllegalArgumentException: Inconsistent arguments and signature at org.apache.tomcat.util.modeler.ManagedBean.getInvoke(ManagedBean.java:578) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:289) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown Source) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source) 

What am I doing wrong?

1 Answer 1

2

I see you are doing:

Connection myConnection = (Connection) server.invoke (mBean, "getConnection", params, null); 

You are passing in null for the param signature array which I don't think is allowed. To quote the javadocs from MbeanServer.invoke(...):

@param signature An array containing the signature of the operation. The class objects will be loaded using the same class loader as the one used for loading the MBean on which the operation was invoked.

This array should hold the class names of the method parameters you are trying to invoke and they must match precisely. Primitive types should be passed in as the string "int", "long", ... while class types as "java.util.Date", "java.lang.String", ...

So I think you need to pass in something like:

String [] params = {"<username>", "<password>"}; String [] signatures = {"java.lang.String", "java.lang.String"}; Connection myConnection = (Connection) server.invoke (mBean, "getConnection", params, signatures); 
Sign up to request clarification or add additional context in comments.

7 Comments

Yes, this was precisely the problem. Thank you very much...I never would have gotten this on my own. The description of the signature parameter is Greek to me.
Glad to help @Dan. I've expanded my answer to provide a better definition of the signature argument.
Thank you, Gray. I wasn't trying to imply that the explanation you gave was unclear; in fact, it was quite helpful. Given the example String array "signatures" that you provided, I was able to divine what the parameter "String[] signature" was supposed to be. I was just trying to state that the javadoc explanation was a little dense and hard to understand. Nonetheless, thanks for further clarifying your answer.
I would like to point out that, given this example, both parameters "String[] params" and "String[] signature" should be null. Although the MBean ObjectName indicates the object type is DataSource, at runtime it is actually a BasicDataSource, which does not support the getConnection(String username, String password) method. Running it without any parameters will return a valid connection.
Sure thing. I realized that my answer didn't fully explain it for others. Folks forget @Dan that these answers are for you and posterity.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.