• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unable to catch EJBException from ejbstore in facade

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your session bean is remote, right? As TransactionRolledbackException IS-A RemoteException, your code should be catching it.
 
shailesh Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
shailesh Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic