[spring] transaction이 readonly인지 확인

by 스뎅(thDeng) on

현재 사용 중인 transaction이 정말 readonly로 설정되었는지 확인하고 싶을 때, TransactionSynchronizationManager#isCurrentTransactionReadOnly를 사용해서 확인이 가능하다.

import org.springframework.transaction.support.TransactionSynchronizationManager;


@Transactional(readOnly = true)
public void testReadOnly() {
    log.info("Transaction readonly flag: {}",
        TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    // do my task
}

TransactionSynchronizationManager를 통해서 isolation level(getCurrentTransactionIsolationLevel) 이나 transaction 이름(getCurrentTransactionName)도 얻어올 수 있다.

참고

별도로 명시하지 않을 경우, 이 블로그의 포스트는 다음 라이선스에 따라 사용할 수 있습니다: Creative Commons License CC Attribution-NonCommercial-ShareAlike 4.0 International License