0 Replies Latest reply on May 12, 2006 4:25 AM by chaituu

    how to rollback the transaction in ejbStore()

    chaituu

      we are using mvc-2 architecture.when i want to modify the employee details what we r doing is from controller it will goes to modifyEmployee method in sessionbean.as u can see in code we will setEmployeeMaster inturn it will call setEmployeeMaster in Entity bean ;we r using EJB1.1;


      i am checking whether sizes of empcharges and deptcharges does not equal then throw exception and total transaction should be rollback.

      At the time of reterving the count of two tables even data is not inserted in two tables in ejbStore() because ejbStore will be called by the container;after executing full modifyEmployee() method only container is calling ejbStore where i am inserting data in two tables;

      my requirment is i have to check whether in two tables any mismatch is going ;

      how can i write this in this type of scenario?but while creating employee details i have put same condition where ejbCreate will be called at that time transaction is rolledback.(when i say home = (EmployeeEntityHome)ictx.lookup("EmployeeEntityBean");
      remote = (EmployeeEntity)home.create(); container is calling immediately ejbCreate);


      but in this case container is not calling immediately ejbStore when i say remote.setEmployeeMaster(employeeMaster);



      i have written Pseudo code may be some errors are there;i am using JBoss




      Session bean
      **********

      modifyEmployee(EmpMaster empMaster) thrpws MismatchException
      {


      try
      {
      *************other business processes******************

      ictx = new InitialContext();
      home = (EmployeeEntityHome)ictx.lookup("EmployeeEntityBean");
      remote = (EmployeeEntity)home.findByPrimaryKey(new Long(empMaster.empNo));
      remote.setEmployeeMaster(employeeMaster);

      *************other business processes******************

      query1="select count(*) from empcharges where empno='*****'"
      query2="select count(*) from deptcharges where deptpno='*****'"

      rs=stmt.executeQuery(query1);
      rs1=stmt.executeQuery(query2);

      if(rs.next)
      int cnt1=rs.getInt(1);

      if(rs1.next)
      int cnt2=rs.getInt(1);

      if(cnt1 != cnt2)
      {
      flag =true;
      }

      if(flag)
      throw new MismatchException("mismatch exception ");

      }
      catch(Exception e)
      {
      ctx.rollBackOnly();
      }
      }

      EntityBean
      ***********

      public void setEmployeeMaster(EmployeeMasterDOB employeeMasterDOB)
      {
      this.employeeMasterDOB = employeeMasterDOB;
      isDirty = true;
      }

      public void ejbStore() throws javax.ejb.EJBException
      {
      try
      {
      if(isDirty)
      {
      isDirty = false;
      EmployeeMaster EmployeeMasterDAO = new EmployeeMaster();
      EmployeeMasterDAO.store(employeeMasterDOB);
      }

      }
      catch(Exception ex)
      {
      throw new EJBException(ex.getMessage());
      }
      }

      EmployeeMasterDAO
      ****************

      store(employeeMasterDOB)
      {
      data will be inserted in empcharges and deptcharges tables...
      }