I had the impression that: -NoSuchEntityException would be thrown when a BMP or CMP tries to access an entity, which has been already removed from underlying persistent store.
- and NoSuchObjectException would be thrown when the client calls a method, but the EJBObject has been removed from server.
But, the section 18.3.4 of the spec, titled "javax.ejb.NoSuchEntityException", says following, which is confusing me:
To give the client a better indication of the cause of the error, the Container should throw the java.rmi.NoSuchObjectException (which is a subclass of java.rmi.RemoteException) to a remote client, or the javax.ejb.NoSuchObjectLocalException to a local client.
Does this mean that when the container detects that the entity has been removed, it will destroy the EJBObject???
NoSuchEntityException is only for BMP beans. It is an exception that a BMP bean throws to the container to tell the container that the entity it is looking for is no longer present in the database. Clients will never see this exception. It is not for CMP beans because in CMP, the bean will not look for any entity in its code, its the container that implements the finder methods for this purpose.
For your second question, if an entity is removed through the EJB application, ie someone calls remove() method, then the entity will be deleted and the EJBObject for that entity will be tossed out (made eligible for garbage collection). But the client might still have an active stub to that EJBobject. Calling any more methods on that stub will throw a java.rmi.NoSuchObjectException. But if the entity is removed externally outside of the EJB application, then the EJBObject may still be around, but invoking any method call on it will again result in the same exception -NoSuchObjectException for remote clients and NoSuchObjectLocalException for local clients
I tried this scenario with a sample CMP bean ... on J2EE RI. I tried two scenarios: - Client finds a entity using findByPrimaryKey, then I deleted the entity from database externally outside EJB and then had the client call a business method on component interface reference aquired through findByPk - Client finds a entity using findByPrimaryKey, then client called remove using component interface reference, then client call business method on the same component interface.
In both the above cases, the client got NoSuchObjectException at the last step. For second scenario, I understood the whole thing.
But I dont understand why the client gets NoSuchObjectException for first scenario - ideally, the EJBObject should be on the server side (because entity has been deleted outside of EJB) and if it really exists on the server side, then why does client get NoSuchObjectException?
The NoSuchEntityException is never saw by the client.
It's an exception that the bean (BMP) throws to Container when data is lost. The container, according to specs, should send back an EJBException to the client, and more specifically, it SHOULD send a NoSuchObjectException.
So even if a NoSuchEntityException is raised, the client will see a NoSuchObjectException.
From specs :
The NoSuchEntityException is a subclass of EJBException. If it is thrown by a method of an entity bean class, the Container must handle the exception using the rules for EJBException described in Sections 18.3.1, 18.3.2, and 18.3.3. To give the client a better indication of the cause of the error, the Container should throw the java.rmi.NoSuchObjectException (which is a subclass of java.rmi.RemoteException) to a remote client, or the javax.ejb.NoSuchObjectLocalException to a local client.