配置事務管理器git
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 引用數據源bean --> <property name="dataSource" ref="dataSource"/> </bean>
配置事務的通知github
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice>
<aop:pointcut id="txPc" expression="execution(* cn.ann.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
使用tx:advice標籤內部的tx:attributes標籤配置事務屬性spring
<tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> </tx:attributes>
配置通用切入點表達式:數據庫
@Pointcut("execution(* cn.ann.service..*.*(..))") public void txPc(){}
爲每個須要配置事務的方法配置事務的屬性express
@Transactional(propagation = Propagation.REQUIRED, readOnly = false) public void transfer(String source, String target, Double money) { ... } @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) public Account getAccountByName(String name) throws SQLException { ... } @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) public Account getAccountById(Integer id) throws SQLException { ... }
建立事務管理器bean編程
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
建立事務模板beanide
<bean id="txTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean>
編寫業務代碼code
@Service("accountService") public class AccountServiceImpl implements AccountService { @Resource(name = "txTemplate") private TransactionTemplate txTemplate; @Resource(name = "accountDao") private AccountDao dao; @Override public void transfer(String source, String target, Double money) { txTemplate.execute(status -> { try { Account sourceAccount = dao.getAccountByName(source); Account targetAccount = dao.getAccountByName(target); sourceAccount.setAccountMoney(sourceAccount.getAccountMoney() - money); dao.updateAccount(sourceAccount); // int i = 1 / 0; targetAccount.setAccountMoney(targetAccount.getAccountMoney() + money); dao.updateAccount(targetAccount); } catch (SQLException e) { e.printStackTrace(); } return null; }); } @Override public Account getAccountById(Integer id) throws SQLException { return txTemplate.execute(status -> { try { return dao.getAccountById(id); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } }); } @Override public Account getAccountByName(String name) throws SQLException { return txTemplate.execute(status -> { try { return dao.getAccountByName(name); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } }); } }
在開發中, 關於用註解仍是配置文件: 在公司沒有硬性要求的狀況下, 怎麼方便怎麼來. 我通常用配置文件配置事務
本片代碼: 此處 的全部spring04xml