Search code examples
javaspringjdbcspring-transactions

Spring Transactions Flow


Need to understand if all the three flows of lockData are in one transaction. Can I see the row lock added by Class C1 on table employee when the flow comes back to Class A1?

Flow currently is: An instance of A1 calls an instance of B1 which in turn calls an instance of C1

class A1 {
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void lockData(){ 
        B1 classBObj = ctx.getBean("B1");
        boolean locked = classBObj.lockData();      
        //Check if lock on employee table is still there.
    }   
}

class B1 {
    @Transactional(propagation = Propagation.REQUIRED)
    public void lockData(){ 
        C1 classCObj = ctx.getBean("C1");
        classCObj.lockData();   
        return true;
    }
}

class C1 {  
    public void lockData(){ 
        executeQuery("select * from employee where emp_id=1 for update");
        return true;
    }
}

Solution

  • If the instance of A1 used is a spring-bean and injected through the spring bean context then yes, a call to a1.lockData() (and all its nested calls) will be executed in one transaction.

    For completeness sake, I would suggest annotating C1:lockData with @Transactional(propagation = Propagation.REQUIRED).