简体   繁体   中英

In spring declarative transaction management when transaction commited

As describe in question , where actually transaction get committed in spring desclarative transaction mangament. For eg suppose i have following code

@Service
@Transactional
class CustomerAService{
    public void processCustomer(Customer customer){
        //call dao and insert customer
        furtherProcessCustomer(Customer customer);
        //last line (a)
    }

    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void furtherProcessCustomer(Customer customer){
        //call another dao do some db work

        //last line (b)
    }
}

suppose if i stop execution @ line //last line (a) so will trasaction get commited for processCustomer() method. I tried to search on net but didn't get much information

The transaction management in spring occurs via aspect-oriented-programming (AOP) proxy objects. This means that the method must return cleanly in order for the transaction to be committed. Your code is "Target Method" in the diagram below, while the transactions are committed in the "Transaction Advisor". More documentation here.

AOP交易处理

Your example is kind of subtle since furtherProcessCustomer method is called from inside the same class, which will not be called via the AOP proxy object and therefore your @Transactional(propagation=Propagation.REQUIRES_NEW) will not be used.

If you had another service, also with @Transactional annotations and then you called furtherProcessCustomer , this would occur via the AOP Proxy object and you would therefore have a nested transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM