aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes unable to catch EJBException from ejbstore in facade Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "unable to catch EJBException from ejbstore in facade" Watch "unable to catch EJBException from ejbstore in facade" New topic
Author

unable to catch EJBException from ejbstore in facade

shailesh Kumar
Greenhorn

Joined: Aug 12, 2005
Posts: 8
Hi,

I have a simple stateless session bean that has a method
that tries to access an entity bean, The entity bean has all method empty except ejbstore.

public void ejbStore() {
System.out.println("EntityBean.ejbStore()");
throw new EJBException("Lets see if i can catch this");
}

the method in session bean is :

public String testEjbException(){

Context ctx;
try {
ctx = new InitialContext();
EntityHome home = (EntityHome)ctx.lookup("ejb/com/EntityHome");
home.create();

return "Created";
} catch (NamingException e) {
System.out.println(" Naming Exception() NamingException");
return "NamingException";
} catch (RemoteException e) {
System.out.println(" RemoteException");
return "RemoteException";
} catch (CreateException e) {

System.out.println(" CreateException");
return "CreateException";
}
catch (NoSuchEntityException e) {

System.out.println(" NoSuchEntityException");
return "EJBException";
}catch (EJBException e) {

System.out.println("EJBException");
return "EJBException";
}

}

But i am not able to catch this exception in the session bean.
Instead i get a TransactionRolledbackException Exception.

I am using WSAD 5.1.2

Can we catch EjbException this way?

Thanks and regards,
Shailesh
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Your session bean is remote, right? As TransactionRolledbackException IS-A RemoteException, your code should be catching it.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
shailesh Kumar
Greenhorn

Joined: Aug 12, 2005
Posts: 8
Hi,

True, I should be able to catch the remoteException,
But the problem is that ....

Session Bean is running in "Requires New"

If i change this to "Not Required" , I can catch the Remote Exception.

But I cant do it because ...

I have to insert into one table (using entity) ...
Then log, that the insert is successfull in other table...

Any of the above two fails I need to rollback ...

I guess , the problem is EJBException marks transaction for rollback...
and when the session bean receives another exception...
It again trys to mark the transaction for rollback ...
This results in error .....

Just a guess,

Thanks and regards,
Shailesh
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Set the transaction attribute of the entity bean's method to Required. This will solve your problem as both updates will run in the same transaction.
shailesh Kumar
Greenhorn

Joined: Aug 12, 2005
Posts: 8
Hi,

My Entity does have "Requires" set,

My Exact problem is that, the database has a "unique" constraint set on it, So its failure is revealed only when ejbstore is called.

When this cosntraint is viaolated i throw an EJBException("Unique cosntraint violated").

But The remote Exception when caught , always gives me transactionRollBackException, i tried remoteExeption.detail as well.

So, the problems are :

1. Catch the RemoteException when SessionBean is running "requires new" and entity "requires" (works in WSAD 5.0, but i dont know why,it doesnt on WSAD 5.1.2)

2. Catch the Message that i set in EJBException. ( i want to see the message , because there are two unique constraints and i need to diffrenciate between the same)

Regards,
Shailesh
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"shailesh shailesh",
welcome to the Ranch!

There aren't may rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
Dave
shailesh Kumar
Greenhorn

Joined: Aug 12, 2005
Posts: 8
Hi,


To catch the exception
1. we had to create an intermediate method with "No transaction" and from there call all methods with "requires"

2. But we always get "transaction roll back exception" , we are unable to retrieve a custom message set in EJBException

Regards,
Shailesh
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The container is not obliged to encapsulate EJBException in RemoteException. What you can do is invoke setRollbackOnly and then throw a custom application exception (instead of throwing EJBException).
Veshnu Ramakrishnan
Ranch Hand

Joined: Sep 27, 2005
Posts: 44
hi shailesh
The ejbStore methods is called by the container and not by you. The problem occurs here, when ever you update any variables in your entity bean the container calls that beans ejbStore method to persists itself with the database. so as you throw an exception in this method the container will assume that the transaction is been interupted and will automatically roll back the transaction and throws a TransactionRollBackException to intimate the client that there is a problem with that transaction and so it has been rolledback. That's why you get a TransactionRolledBackException instead of EJBException which you threw inside the ejbStore() method.


Veshnu<br />SCBCD<br />SCWCD<br />SCJP
shailesh Kumar
Greenhorn

Joined: Aug 12, 2005
Posts: 8
Hi,

Hasnt anyone found the need to catch the exception in ejbStore and communicate to client the exact reason of failure?

If i can catch only remote exception, and the e.detail.getMessage
or e.getMessage all return me only "Transaction roll back exception" .

How can i inform the client that the error is because of
1. business condition failure (in my case, unique constraint failure, i need to say to client " your record already exist" ) or

2. some other failure (unable to get connection to db, i need to say to client "please try after some time").


The only workarround I see to use direct sqls instead of entity's setter methods, But then; what is the use of the entity bean?


Does this mean that when entities are being used, we are not using any constriants on the database?

Do BEA or Jboss handle this need?


Thank you for all your replies,

Regards,
Shailesh
Veshnu Ramakrishnan
Ranch Hand

Joined: Sep 27, 2005
Posts: 44
As per EJB Spec, if you are trying to store two records with same key in a database it throws a DuplicateKeyException. if you want to check it yourself with your own sql, try throwing an application exception rather than remote exception from your code inside ejbStore.

Throwing RemoteException(RuntimeException) will always rolls back the transaction and throws TransactionRollBack exception
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to catch EJBException from ejbstore in facade