I am new EJB framework and am in the process of learning it. I am developing a standalone application using EJB3.1 on Eclipse IDE and Glassfish 3 as server. Below is the code snippet.
@Remote
public interface DataSourceRemote {
public Connection getConnection();
}
@Stateless(mappedName="ejb/datasource")
public class DataSourceRemoteBean implements DataSourceRemote{
@Resource(name="jdbc/datasourceDB")
DataSource ds;
public Connection getConnection() {
try {
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
//My client code goes here
public class Client {
public static void main(String args[]) {
try{
InitialContext ctx = new InitialContext();
DataSourceRemote bean =(DataSourceRemote)ctx.lookup("com.global.entities.DataSourceRemoteBean");
Connection conn = bean.getConnection();
if(null==conn){
System.out.println("its null");
}else{
System.out.println("connection established:"+conn.toString());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
When ever I try to do a JNDI look up for jdbc/datasourceDB in cleint it works fine, but when I try to do a look up on ejb/datasource and call getConnection() it throws me an error. Below is the stack trace
javax.ejb.EJBException: java.rmi.MarshalException: CORBA MARSHAL 1330446343 No; nested exception is:
org.omg.CORBA.MARSHAL: FINE: IOP00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at com.global.entities._DataSourceRemote_Wrapper.getConnection(com/global/entities/_DataSourceRemote_Wrapper.java)
at com.global.client.Client.main(Client.java:24)
Caused by: java.rmi.MarshalException: CORBA MARSHAL 1330446343 No; nested exception is:
org.omg.CORBA.MARSHAL: FINE: IOP00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:267)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:213)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:152)
at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:227)
at com.global.entities.__DataSourceRemote_Remote_DynamicStub.getConnection(com/global/entities/__DataSourceRemote_Remote_DynamicStub.java)
... 2 more
Caused by: org.omg.CORBA.MARSHAL: FINE: IOP00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:248)
at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:95)
at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.handleFullLogging(WrapperGenerator.java:387)
at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.access$400(WrapperGenerator.java:107)
at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator$2.invoke(WrapperGenerator.java:511)
at com.sun.corba.ee.spi.orbutil.proxy.CompositeInvocationHandlerImpl.invoke(CompositeInvocationHandlerImpl.java:99)
at $Proxy24.endOfStream(Unknown Source)
at com.sun.corba.ee.impl.encoding.BufferManagerReadStream.underflow(BufferManagerReadStream.java:128)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_1.grow(CDRInputStream_1_1.java:113)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_2.alignAndCheck(CDRInputStream_1_2.java:126)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.read_long(CDRInputStream_1_0.java:496)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.readValueTag(CDRInputStream_1_0.java:1810)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.read_value(CDRInputStream_1_0.java:1040)
at com.sun.corba.ee.impl.encoding.CDRInputObject.read_value(CDRInputObject.java:531)
at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$14.read(DynamicMethodMarshallerImpl.java:384)
at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.readResult(DynamicMethodMarshallerImpl.java:483)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:203)
... 5 more
Am I missing some thing?
Can anyone please help me?
If you’re using a Remote interface, execute a standalone application and use the JNDI to do the lookup than you’re sending the data over the wire (in other words – it’s not a local call).
I don’t think you should send app server DataSource Connection to the client. Basically you should left the database access to your EJB on server-side rather than leak this responsibility to the client.
If you’re learning EJBs than you can try with some simple type (Integer, String, etc.).
If it comes to your other question “All container managed objects should not be exposed to the client when remote interface is being used“.
I think it’s more or less true. I don’t think you should expose
UserTransaction,DataSourceorSessionContextcontainer managed objects to the client.However, remember that an JPA entity is also managed by the container, but after detachment – it can be safely sent to the client (and perhaps reconnected/merged when it goes back).
Another example could be a CDI bean. It can be injected by the container and in some cases you can send it to the client and modify it. The container is not able to manage the contextual nature of the CDI bean, but I think that you can still use it.
HTH!