聲明式事務

Declarative transaction management

  • 透過 Spring aspect-oriented programming (AOP),使其能透過 Annotation 使用。

  • 搭配 @Transactional@EnableTransactionManagement (透過@Configuration) 啟用也提供了聲明式事務的回滾規則

  • 可以應用在所有的類別

如何透過 AOP 實現

Core - AOP

透過結合使用 TransactionInterceptor 和 PlatformTransactionManager 實現,

來驅動方法呼叫周圍的事務。

@Transcational

規則

  • 應使用在 公共(Public) 的方法,其他權限不會報錯,但也不會開啟事務。

  • 可以配置在介面,介面上的方法,類別或類別上的公共方法。

  • 不應在 Bean 初始化時使用此功能,即@PostConstruct

預設行為

  • 傳播 (Propagation)PROPAGATION_REQUIRED

  • 孤立度 (Isolation)ISOLATION_DEFAULT

  • 超時 (Timeout)默認與資料庫的超時設置相同,如果不支持超時,則默認為無。

  • 只讀狀態 (Read-only status)false,可讀寫的。

Last updated

Was this helpful?