A misconception about the use of spring transactions

Source: Internet
Author: User

Spring beans are assumed to have the following classes
public class Service Implement iservice{

@Transactional (readOnly = False, propagation=propagation.required)
public void MethodA () {
.....


MethodB ()

......
}

@Transactional (readOnly = False, Propagation=propagation.requires_new)
public void methodb{
......

}


}
public class servicefacade{

@Autowired
private IService service;

pub Lic void Method () {
service.methoda () {

}

}


}
Ask Call Servicefacade Method () methods, what is the MethodA and MethodB business?

in the same transaction or two transactions, does the MethodA transaction take effect or does the METHODB transaction take effect?

The answer is that MethodA's transaction takes effect and MethodB is embedded into the MethodA to become a transaction.

why is that? MethodB's not a statement.propagation.requires_new?


The answer is that spring's transactions are implemented through an AOP dynamic proxy, starting with the spring transaction implementation.


generates a proxy for the bean. Because MethodB is called inside the service, the methodb of the agent Bean is not executed at this time, so the transaction declaration above METHODB is invalidated


when facade invokes the service's MethodA method, it actually executes Serviceproxy.methoda, so the method is taken over by the spring transaction. The transaction declaration takes effect.




The workaround is that the MethodB is placed in another bean by MethodA call or facade call Service.methoda () and then called Service.methodb




Summary: Spring's transactions are implemented based on an AOP dynamic agent. By default, a different method is called in the same class, and the above transaction declaration is not valid.


The above language may not be accurate, but that's probably what it means. If you have a better statement, please let me know, thank you.

A misconception about the use of spring transactions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.