Transcation not Rollbacked in Container Managed Transcation


EJB design: Transcation not Rollbacked in Container Managed Transcation

  1. I have two session bean X,Y which do some database operation.And in my deployment descriptor I have set transcational attribute as Required, as well as transcation type as Container.In this session bean am throwing an
      new java.rmi.RemoteException() if condition is not satisfied(inorder to rollback .Am i right!!).
      Suppose if anyone dosn't satisfies the condition it throws execption.

    I have another M bean which calls the other two
    session bean(X,Y).And in my deployment descriptor I have set transcational attribute as Required, as well as transcation type as Container.

    If any one of two session bean(X,Y) doesn't satisfies the condition it throws exeception.Am caughting in the bean(M)
    and roolbacking like this
              catch(java.rmi.RemoteExcveption e){
                   throws new java.rmi.RemoteException();
    Am i right....
    But I couldn't achieve the transcation concept.I mean even one gets fails other gets committed.

    ie Even if y.method2() throws an exception, database operation on x.method1() is succedding(committed).
    What should I do inorder to work well.

    Anyone help in this regard
    Thanks in advance
    With Regards,
  2. REQUIRES attribute of transaction is standalone . To make it a part of the whole transaction make the other two baens transaction attribute as "SUPPORTED" that way they will support the calling bean's transaction.
    vivek sharan
  3. What is your app server? Database? JDBC driver? Are you using the same database connection pool in beans X and Y?
  4. Applicaton server is weblogic.Database sybase sqlserver.
    Driver is jconnect4.2.Am using same connection pool on the
    X and y beans.
  5. I think that instead of throwing the RemoteException, you should throw the EJBException. In order to make sure that Container rollbacks your transaction, you must throw the System exception. Once you throw the EJBException then container will rollback your transaction and wrap your exception into the RemoteException and throw to the client.

    throw new EJBException("Exception: " + ex.getMessage());
    Another thing you can do is:
    before throwing the exception call setRollBackOnly on the SessionContext instance.

    I hope this will help...

  6. M bean

    public void setSessionContext(SessionContext ctx){
    this.ctx = ctx;

    public boolean transferSTtoCT(String name1,String name2,int amount) throws EJBException{

     CitiBankHome citiHome=(CitiBankHome)ic.lookup("citicorp");
     CitiBank citiRemote=citiHome.create();
     StandardHome standHome=(StandardHome)ic.lookup("standard");
     Standard standRemote=standHome.create();

    catch(EJBException re){
       throw new EJBException("Transcation "+re.getMessage());
    catch(Exception e){
       System.out.println("Exception in lookup"+e);

    M bean ejb-jar file


    X bean

    public boolean credit(String name,int amount) throws EJBException{
         //loading driver
        sql="select citibank where name='"+name+"'";
        java.sql.ResultSet result=null;
       catch(Exception e){
       if(temp == 0){
    throw new EJBException();
       sql="update citibank set amount="+amount+" where
        PreparedStatement pstmt=con.prepareStatement(sql);
      catch(Exception e){
      if(flag == 1)
    return true;
      return false;

    X bean's ejb-jar


    Y bean
    same as X bean but this will debit the amout

    y bean's ejb-jar


    I couldn't able to accomplish the transcation concept.May anyone tell me where I have made mistake
    Thanks in advance.
    with Regards,
  7. Hi,
      you r not able to acheive the transaction integrity between the two methods, because, the two methods are not running in the same transaction context.

      Remember that the 'REQUIRED' attribute makes the container start a new transaction if the client doesn't supply one. which is exactly happening in ur case.

      The solution for ur problem wud be to start a client transaction in ur M bean.

      javax.transaction.UserTransaction utx = ctx.lookup


    }catch(Exception e){

    hope this helps.
    Manohar(manoharm at planetasia dot com)
  8. Doess "citicorp" points to a transactional datasource specified in the weblogic 5.1's properties file? WLS 5.1 does not support distributed transactions, and I don't know whether this is the case (my understanding is you have to be using 2 databases for the transaction to be considered distributed). Have you tried a different JDBC driver other than JConnect (I am not familiar with Sybase)? How about simply re-throw the EJB exception in your first catch block and see whether the transaction gets rolled back and go from there.