Discussions

EJB programming & troubleshooting: e.getMessage() across two Session beans shows entire stack trace

  1. We are using Stateless Session Beans (EJB 1.1 ) , WEBLOGIC 5.1 SP 8, on Solaris.
     From a function f1 of a Session Bean say S1 we are calling function f2 of Session Bean S2. In the function f2 at some error condition we throw an exception like
         throw new EJBException("This is a customized error message !!") ;

    The same is caught in f1 (of S1) and we are trying to display the customized error message we had set inside f2 (of S2).
      e.g
         catch(EJBException ejbE)
    {
                            ----------------------
    System.out.println(ejbE.getMessage());
    }
      But here if we try to print this message (actually we are setting it in our customized class to display it on the screen, but for test purposes we are using System.out.println.....)
     Now instead of simply getting the message , here we are getting the entire stack trace alongwith our customized message which is not desired. If we do the same thing but with the condition that both these functions are within the same Session Bean , everything works fine i.e we get only the customized error message set inside function f2.
     Any suggestions how to solve it ???
     Thnx in advance.

    Sajan Parihar
  2. Can you post the code to this thread. It sounds odd, but is hard to diagnose without seeing the code.

    Chz

    Tony
  3.  We are using Container managed transaction in Weblogic 5.1 SP8.

    I am calling the Session Bean from a Java Bean, and in case some error business rule validation or some System error occurs we throw an EJBException with some customized error meesage , which is later on set into a Customized Error Class in the Java Bean.
       The sample pseudo code is written at the end of this message.
     I am using say, two functions insertData and isDuplicateKey in Session Bean "SesBOne".
     Before carring out any insertions to the database in insertData() some business checks are done in isDuplicateKey()
     ,and in case some error condition is found we raise EJBException with a customized message and the transaction gets rolled back.
     The Customized error mesg is retreived in Java Bean by calling something like e.getMessage() and then set in a Error Class and shown on the screen.
     Everything here works fine as long as the function isDuplicateKey is called from within the same Session Bean.

     But in case the function isDuplicateKey is called from some other Session Bean, in case of a error condtion the transaction gets rolled
     back and the error message also gets transferred to the calling Session Bean, but not alone alongwith the Customized error message we are
     getting the complete stack trace when I am calling ejb.getMessage(), which logically should be returning only the error message
     set in.

     I just want to know why it is happening so. Why this difference of behaviour ???? I have tried it quite a number of times ...may be
     I am doing something terribly wrong ...Pls. rectify me !!
      Looking forward to hear ur comments/views.

      Besides throwing an EJBException to rollback a transaction (in Container managed transaction environment), is there some other
      "Better Way" to do this ??? If it is there, no doubt I would like to know that but I will be still interested to know the answer
      to my problem.

      thnx in advance.

      Sajan Parihar


       The sample pseudocode is written below :





    /**************************sample Pseudo Code ***********************************************/
    public class SesBOne implements SessionBean
    {
    public String insertData(HashMap h) throws EJBException
    {
    String amt;

    try
    {

    isDuplicateKey((String) h.get("var1"), (String) h.get("var2"), (String) h.get("var3"));

    -------perform insertions

    }
    catch (SQLException sqlE)
    {
    throw new EJBException(sqlE.getMessage());
    }
    catch (EJBException ejbE)
    {
    throw new EJBException(ejbE.getMessage());
    }

    catch (Exception expE)
    {
    throw new EJBException(expE.getMessage());
    }
    return amt ;
    }

    public void isDuplicateKey(String var1, String var2, String var3) throws EJBException
    {
    try
    {

    ---- perform some business checks
    ---- if results are negative rollback the Transcation by throwing EJBException and with a customized mesg

    --------
    if (chk1)
    {
    throw new EJBException("Customized mesg AAAAAAAAAAAAAAAA");
    }
    else if(chk2)
    {
    --------
    throw new EJBException("Customized mesg ........AAAAAAAAAAAAAAAA");
    }



    catch(SQLException sqlE)
    {
    throw new EJBException(sqlE.getMessage());
    }
    catch(EJBException ejbE)
    {
    throw new EJBException(ejbE.getMessage()) ;
    }

    catch(Exception expE)
    {
    throw new EJBException("Error ........");
    }

    return ;
    }

    }

  4. The exception problem is strange, I'll have to play around to see if I can replicate it.

    As to rolling back transactions, the best way to do this is to call setRollBackOnly from within your bean. This tells the container that the only thing possible is to roll back the transaction.

    Hope that helps

    Chz

    Tony
  5. even if we call
      void setRollbackOnly()
    which doesn't returns anything how can we send a customized message from the Session Bean citing the reason why the transaction was rolled back.
     If instead we use
        throw new EJBException("Custiomized mesg")
    we can very well send back our customized mesg to the Java Bean and then we can set it into our Error Class.
     But then the problem we are facing with this approach is that when we call another Session Bean and deliberately raise a exception there, we just don't get only the customized mesg , but the full stack trace which is irritating .....any suggestion how to use EJBException across two Session Beans ???????????????/
     
  6. No, I still need to think about that one.

    But even then, I still think calling setRollBackOnly is the better approach. It might not return an error message, but you could set an error message somewhere else in your bean which you could extract if the transaction is rolled back.

    Either way, using Exceptions to control program flow is not a good idea. Leave them to the exceptionals.

    Your particular situation is arguable on the border between the two, so it's really your call as to whether this is program flow, or an exceptional situation.

    Either way, none of this answers your original question, on which I need to spend some time playing.

    Will get back to you eventually, promise!

    Chz

    Tony
  7. any updates on my problem ????