0

Hi I need the following clarification on spring transaction. Seen is like:

Service1.java

@Service
@Repository
public class Service1 {

    @Autowired
    EmployeeRepo employeeRepo;

    @Autowired
    Repo repo;

    @Transactional
    public void m() {

        employeeRepo.save(new Employee(5, "N3", 33, 3000));
        repo.m1();  //M1
        //m1();       //M2
        System.out.println(1/0);   //M3
    }

    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void m1() {

        Employee e=employeeRepo.findOne(1);
        //System.out.println(1/0);  //M4
        e.setAge(212);
    }   
}

Repo.java

@Repository
class Repo{

    @Autowired
    EmployeeRepo employeeRepo;

    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void m1() {

        Employee e=employeeRepo.findOne(1);
        //System.out.println(1/0);  //M5
        e.setAge(212);

    }

}

cases:

When executing as it is then no insertion but only update is done with exception 2.(reset)When commenting M1 and un commenting M2 then no insertion no update is done with exception (method in same class)

3.(reset)When uncommenting M5 and commenting M3 then no insertion no update is done with exception

4.(reset)When uncommenting M2,M4 and commenting M1,M3 then no insertion no update is done with exception

NOTE: same m1() method is in same caller calss and in diffrent class also. So why is this different behaviour of same operation in diffrent class? What is the concept then?

Joe
  • 479
  • 3
  • 8
  • 31
  • Possible duplicate of [Spring @Transactional Annotation : Self Invocation](https://stackoverflow.com/questions/23931698/spring-transactional-annotation-self-invocation) – Ioan M Apr 25 '19 at 14:20
  • please check this post why it is not working https://stackoverflow.com/questions/23931698/spring-transactional-annotation-self-invocation But in later Spring releases you can do self autowiring and then call via the autowired member the transactional method and even tough it is in the same class the proxying and '@Transactional' should work https://stackoverflow.com/questions/5152686/self-injection-with-spring – Ioan M Apr 25 '19 at 14:25

0 Answers0