就使用者角度來說,所謂的事務主要分兩方面:
開啓事務:
說明式事務:
Spring mvc(傳統web項目):
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 開啓聲明式事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
Spring boot:
@EnableTransactionManagement // 啓註解事務管理,等同於xml配置方式的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication {
// 其中 dataSource 框架會自動爲我們注入
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
編程式事務:
同說明式事務,可以優先定義TransactionTemplate, 也可以在程序中手動加載
使用事務:
說明式事務:
@Transactional
編程式事務:
TransactionTemplate:
PlatformTransactionManager:
進一步瞭解:
http://www.importnew.com/18332.html