Spring Cloud微服務(wù)框架,實戰(zhàn)企業(yè)級優(yōu)惠券系統(tǒng)
public void transactionTest() {
? ?// 開啟事務(wù)
? ?TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
? ?try {
? ? ? ?// ----------執(zhí)行數(shù)據(jù)庫操作----------start
? ? ? ?List<CMPhone> list = new ArrayList<>();
? ? ? ?CMPhone cmPhone = new CMPhone();
? ? ? ?cmPhone.setPhoneFirst("X1");
? ? ? ?list.add(cmPhone);
? ? ? ?cmPhoneMapper.insertBatch(list);
? ? ? ?// 如事務(wù)寫入的數(shù)據(jù)很多,且執(zhí)行時間太長,可中途進行資源刷新(刷新緩存到數(shù)據(jù)庫,提高速度)
? ? ? ?transactionStatus.flush();
? ? ? ?cmPhoneMapper.insertBatch(list);
? ? ? ?// ----------執(zhí)行數(shù)據(jù)庫操作----------end
? ? ? ?// 提交事務(wù)
? ? ? ?transactionManager.commit(transactionStatus);
? ?} catch (Exception e) {
? ? ? ?e.printStackTrace();
? ? ? ?// 回滾事務(wù)
? ? ? ?transactionManager.rollback(transactionStatus);
? ?}