針對jdbc中的事務管理,在springDAO中同樣有着事務管理模式。
其分兩種:
編程式事務管理
聲明式事務管理
前者不多用,後者方便故多用,本文就後者進行描述。
<1>在applicationContext.xml配置文件中開啓註解形式的聲明事務管理
<!-- 開啓聲明式事務 -->
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true" />
<!-- 配置一個基於數據源的事務管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<2>在需要添加事務管理的地方添加@Transactional
@Service
public class BankService {
/** 轉賬的方法
@Transactional(rollbackFor={BankAccountException.class},
isolation=Isolation.SERIALIZABLE,propagation=Propagation.REQUIRED)
public void transfer(BankAccount from,BankAccount to,double money) throws BankAccountException{
望能對小夥伴對事務管理理解 有所幫助!