리팩토링(스프링기반)
private method의 @Transactional 제거
까오기
2019. 1. 8. 09:26
"Make this method "public" or remove the "@Transactional" annotation"
@Transactional(propagation=Propagation.REQUIRED, readOnly=false, rollbackFor=Exception.class) <-- 제거
private void insertCommon(StoreDetail pStoreDetail, String trgStore) {
// ...
}
private method에 @Transactional 이 붙어 있는 소스입니다. AOP를 이해 못하시는 분들이 이런 실수를 가끔하시면서 "@Transactional이 불안정하다.", "작동을 안한다"라는 얘기를 하십니다. 꼭 @Transactional이 아니라 cache나 다른 애노테이션도 마찬가지입니다.
컨트롤러에서 서비스를 호출하거나 서비스에서 리포지토리를 이용할 때 AOP가 전처리 또는 후처리를 하는 것입니다. 컴포넌트 내부에서 데이터를 주고 받거나 처리하는 경우 이런 애노테이션은 작동하지 않습니다. 이런 소스를 발견하게 되면 지우고 개발자에게 설명을 해주는게 좋습니다. 그대로 두면 실수를 할 수 있는 여지가 있습니다.
Non-public methods should not be "@Transactional" (squid:S2230)
Marking a non-public method @Transactional
is both useless and misleading because Spring doesn't "see" non-public
methods, and so makes no provision for their proper invocation. Nor does Spring make provision for the methods invoked by the method it called.
Therefore marking a private
method, for instance, @Transactional
can only result in a runtime error or exception if the method is actually written to be @Transactional
.
Noncompliant Code Example
@Transactional // Noncompliant private void doTheThing(ArgClass arg) { // ... }