-2

i am trying to achieve spring boot Transaction service layer bit not able to achieve:

Here is my code


@Service
public class EmpService {

    @Autowired
    EmployeeDao employeeDao;
    
    @Autowired
    EmployeeRepo empRepo;
    
    @Autowired
    EmployeeHealthInsuranceRepo healthRepo;
    
    @Transactional
    public void insertEmployee1(Employee employee,EmployeeHealthInsurance employeeHealthInsurance) {
        
            empRepo.save(employee);
            System.out.println(100/0);
            healthRepo.save(employeeHealthInsurance);
        
    
    
    }
    
    
    public void insertEmployee(Employee employee,EmployeeHealthInsurance employeeHealthInsurance) {
        insertEmployee1(employee,employeeHealthInsurance);

        }
}

i am expecting roll back if any error occuer from database or any code level

1 Answers1

1

The issue happens when you call insertEmployee1(...) from insertEmployee(...)

The @Transactional annotation does not extend to the insertEmployee(...) method.

Try adding @Transactional above insertEmployee(...) for it to be transactional too.

Hope it helps.

Phil Ku
  • 191
  • 1
  • 6