-1

I have 3 classes and a few methods, class A has a method with

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)

from this method, I call class2 > methodA
from this method class3 methodA is called and in this method DB Insert operation is done.

In class2 methodA there is an exception so that I have to roll back the entire transaction happened in Class3 methodA.

but nothing is rolling back. Is there anything I am missing here or my understanding of @Transactional Propagation is wrong?

Class A

  @Transactional(propagation = Propagation.REQUIRED, 
    rollbackFor = Exception.class)
public class classA {


    public DTObject callUpdateValues(DTObject _dtoobject) throws Exception {
        DTObject _resultDTO = null;
        ClassB m = new ClassB();
        _resultDTO= m.updateValues(_dtoobject);
        return _resultDTO;
    }
}

Class B


@Transactional(propagation = Propagation.NESTED,rollbackFor = Exception.class )
public class ClassB {

    String Option;

    public ClassB() {
        super();
        // TODO Auto-generated constructor stub
    }
    public DTObject updateValues(DTObject ClassCMap) throws Exception
    {
                addClassC(ClassCMap);
            if(true)    //custom exception
            {
                throw new Exception("Hello exception");
            }
            ReturnResult.setValue( SUCCESS_FAILURE_KEY ,SUCCESS_VALUE);

      return ReturnResult;
    }



    private void addClassC(DTObject dtoobject) throws Exception
        {
            try
            {

              ClassC ClassC = new ClassC();

              ClassCManager ClassCmanager = new ClassCManager(_COLLECTIONObj,V_LOG_REQ,V_ADD_LOG_REQ);

              ClassC.setClassCId(dtoobject.getValue("ClassC_ID"));

              ClassC.setClassCDescn(dtoobject.getValue("ClassC_DESCN"));

              ClassC.setClassCModule(dtoobject.getValue("ClassC_MODULE"));

              ClassC.setClassCClass(Integer.parseInt(dtoobject.getValue("ClassC_CLASS")));

              ClassC.setClassCProgramIdentifier(dtoobject.getValue("ClassC_PROGRAM_IDENTIFIER"));
              char menuStatus=dtoobject.getValue("ClassC_REQUIRED_IN_MENU").charAt(0);

              ClassC.setClassCRequiredInMenu(menuStatus);

              char finStatus=dtoobject.getValue("ClassC_FIN_NONFIN_OPTION").charAt(0);
              ClassC.setClassCFinNonfinOption(finStatus);

              ClassC.setClassCAuthReqd(stringToChar(dtoobject.getValue("ClassC_AUTH_REQD")));

              ClassC.setClassCDblAuthReqd(stringToChar(dtoobject.getValue("ClassC_DBL_AUTH_REQD")));

              ClassC.setClassCTransitChoice(stringToChar(dtoobject.getValue("ClassC_TRANSIT_CHOICE")));

              ClassC.setClassCUnauthNextDay(stringToChar(dtoobject.getValue("ClassC_UNAUTH_NEXT_DAY")));

              ClassC.setClassCRejAllDuringEod(stringToChar(dtoobject.getValue("ClassC_REJ_ALL_DURING_EOD")));

              ClassC.setClassCTableName(dtoobject.getValue("ClassC_REJ_ALL_DURING_EOD"));



              Set_Entd_Dtls(ClassC);

              ClassC.setIsNew(true);



              ClassCmanager.save(ClassC);
            }catch(Exception e)
                {
                    throw new Exception(e.getLocalizedMessage());
                }
        }


}

Class C


@Transactional(propagation = Propagation.NESTED)
public class ClassCmanager extends Manager
{



    public void save(Mpgm obj, Connection _conn) throws SQLException {
            CRUD operation
    }

}

smootherbug
  • 129
  • 12

2 Answers2

-1

You need to require a transaction as nested in Class2 and Class3 which will use use the Class1 transaction.

// Class1
@Transactional(propagation=Propagation.REQUIRED, ...)
// Class2 & Class3
@Transactional(propagation=Propagation.NESTED)
IQbrod
  • 2,060
  • 1
  • 6
  • 28
-1

If you're not using AspectJ, try making your classes services (annotate with @Service)

Yaroslav
  • 118
  • 1
  • 8