支持事務(wù)的條件:
1, 數(shù)據(jù)庫(kù)的引擎類型必須是INNODE
2, applicationContext.xml配置開啟
<!-- 添加事務(wù)支持 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 開啟事務(wù)注解 --> <tx:annotation-driven transaction-manager="txManager" />
3. 邏輯處理增加事務(wù)注解@Transactional
public class OrdersServiceImpl implements OrdersService{
@Autowired
private OrdersDao ordersDao;
@Transactional
@Override
public int addOrders(Orders orders, int leftMoney) {
ordersDao.addOrders(orders);
if(leftMoney >= 1000){
Orders orders1 = new Orders();
orders1.setUsername("xiaoli");
orders1.setMoney(1000);
orders1.setType(2); //轉(zhuǎn)入
ordersDao.addOrders(orders1);
}
else{
throw new RuntimeException("余額不足,正在滾回");
}
return 0;
}
}4, 以上實(shí)例中可見,有異常,必須拋出異常, 通過(guò)觸發(fā)異常,觸發(fā)回滾
