聲明式事務
Declarative transaction management
透過 Spring aspect-oriented programming (AOP),使其能透過 Annotation 使用。
搭配 @Transactional 與 @EnableTransactionManagement (透過@Configuration) 啟用。 也提供了聲明式事務的回滾規則。
可以應用在所有的類別。
如何透過 AOP 實現
Core - AOP透過結合使用 TransactionInterceptor 和 PlatformTransactionManager 實現,
來驅動方法呼叫周圍的事務。

@Transcational
規則
應使用在 公共(Public) 的方法,其他權限不會報錯,但也不會開啟事務。
可以配置在介面,介面上的方法,類別或類別上的公共方法。
不應在 Bean 初始化時使用此功能,即
@PostConstruct
。
Spring團隊建議:僅使用對類別或類別上的公共方法註解,而不是對介面進行。
您當然可以將@Transactional
註釋放置在介面或其方法上,但僅在使用基於介面的代理時(JDK 動態代理)才可以預期地起作用。
預設行為
傳播 (Propagation):PROPAGATION_REQUIRED。
孤立度 (Isolation):ISOLATION_DEFAULT。
超時 (Timeout):默認與資料庫的超時設置相同,如果不支持超時,則默認為無。
只讀狀態 (Read-only status):false,可讀寫的。
Last updated
Was this helpful?