BREAKING THE EJB LAW ?
Hello, I've been developing JSP's and Servlets, and am just getting in to the realm of EJB's... I am writing a data-driven system in which I want to query method names from a DB and invoke them on my Enterprise bean. I've been using a simpler version of the same code (not using the interfaces) to invoke methods on regular (not Enterprise) beans from a servlet:
InitialContext ctx = new InitialContext ();
Object objRef = ctx.lookup("MyEJB");
MyEJBHome home = (MyEJBHome)javax.rmi.PortableRemoteObject.narrow(objRef, SalaryHome.class);
MyEJB bean = home.create();
Method method = null;
String strString = "Hello";
String methodToCall = "myMethod";
String strResult = "";
try{
method = bean.getClass().getMethod(methodToCall, new Class[]{strString.getClass()});
strResult = (String) method.invoke (bean,new Object[] {strString});
}
The code works fine. I'm just leery that my call to bean.getClass().getMethod() is instantiating a new class and bypassing the object pooling my EJB container is trying to handle for me.
Any help or comments is extremely appreciated.
[1168 byte] By [
HydeNSeeka] at [2007-9-26 12:13:17]

I dont think that the api getClass().getMethod() instantiates a new class.
This api just uses reflection, to gather meta data about the class and the method you want to use.
new instance only gets created if you do a Class.forName().newInstance();
In your code you have obtained a reference for the bean and you are using reflection to invoke the API. you still use the bean reference which the container gave you. so you have not bypassed the pool or anything.
I think you have not broken any law :-)
regards,
Abhishek.
EJB Container is supposed to maintain a pool of bean classes of the (stateless session and entity) beans, but not the EJBObjects. It is upto the vendor how they maintain EJBObjects.
And furthermore, what you get by doing EJBHome.create() is a stub to the actual EJBObject residing on the server. So you are not at all dealing with objects in the container. You are not manipulating it.
In addition when you do bean.getClass().getMethod() it creates an instance of Method class in Reflection package. This is done in your local JVM no on the server. Method object encapsulates the method name and parameters. It is no way instatntiating a bean class. It does not even know where this object is.
Finally you can no way create a instance of bean class (which is in the container) except asking the container to do so.