一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring控制事務的三種方式小結

spring控制事務的三種方式小結

2022-02-12 15:35崇尚至簡 Java教程

這篇文章主要介紹了spring控制事務的三種方式小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

首先準備環境,目錄結構如下

spring控制事務的三種方式小結

數據庫準備

spring控制事務的三種方式小結

業務層代碼

@Service("accountService")
public class AccountServiceImpl implements AccountService {
	@Resource(name = "accountDao")
	AccountDao accountDao;
	public void transfer(Integer from, Integer to, Float money) {
		accountDao.subMoney(from,money);
              int i = 1/0;    //此處引發異常
		accountDao.addMoney(to,money);
	}
}

持久層代碼

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
	public void addMoney(Integer id, Float money) {
		getJdbcTemplate().update("update account set money=money+? where id=?", money , id);
	}
	public void subMoney(Integer id, Float money) {
		getJdbcTemplate().update("update account set money=money-? where id=?", money , id);
	}
}

測試代碼

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
	@Resource(name="accountService")
	private AccountService accountService;
	@org.junit.Test
	public void test(){
		accountService.transfer(1,2,100f);
	}
}

運行結果

spring控制事務的三種方式小結

現在來用三種方式進行事務控制

 

方式一:編碼方式(需要修改源代碼,基本不會用)

添加事務管理類和事務模板類

  <!-- 事務核心管理器,封裝了所有事務操作. 依賴于連接池 -->
  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
      <property name="dataSource" ref="dataSource" ></property>
  </bean>
  <!-- 事務模板對象 -->
  <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
      <property name="transactionManager" ref="transactionManager" ></property>
  </bean>

修改業務層代碼

@Service("accountService")
public class AccountServiceImpl implements AccountService {
	@Resource(name = "accountDao")
	AccountDao accountDao;
	@Resource(name="transactionTemplate")
	private TransactionTemplate transactionTemplate;
	public void transfer(final Integer from, final Integer to, final Float money) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				accountDao.subMoney(from,money);
				int i = 1/0;
				accountDao.addMoney(to,money);
			}
		});

	}
}

 

方式二:xml配置(不需要改動代碼,直接配置xml)

<!-- 配置事務通知 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager" >
      <tx:attributes>
          <!-- 以方法為單位,指定方法應用什么事務屬性
              isolation:隔離級別
              propagation:傳播行為
              read-only:是否只讀
           -->
          <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
          <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
      </tx:attributes>
  </tx:advice>
  <!-- 配置織入 -->
  <aop:config  >
      <!-- 配置切點表達式 -->
      <aop:pointcut expression="execution(* cn.swun.service.*ServiceImpl.*(..))" id="txPc"/>
      <!-- 配置切面 : 通知+切點
               advice-ref:通知的名稱
               pointcut-ref:切點的名稱
       -->
      <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
  </aop:config>

 

方式三:注解

首先開啟注解管理aop事務,然后打注解

  <!-- 開啟使用注解管理aop事務 -->
  <tx:annotation-driven/>
/*
* 該注解可以打在方法上,也可以打在類上
*/
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(final Integer from, final Integer to, final Float money) {
	accountDao.subMoney(from,money);
	int i = 1/0;
	accountDao.addMoney(to,money);
}

 

spring是如何控制事務的?

Spring 的事務,可以說是 Spring AOP 的一種實現。

AOP面向切面編程,即在不修改源代碼的情況下,對原有功能進行擴展,通過代理類來對具體類進行操作。

spring是一個容器,通過spring這個容器來對對象進行管理,根據配置文件來實現spring對對象的管理。

spring的事務聲明有兩種方式,編程式和聲明式。spring主要是通過“聲明式事務”的方式對事務進行管理,即在配置文件中進行聲明,通過AOP將事務切面切入程序,最大的好處是大大減少了代碼量。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_38634814/article/details/82429756

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99久久久久久久 | 成人人免费夜夜视频观看 | 四虎影院在线免费观看视频 | 欧美一区二区三区gg高清影视 | 黄色大片免费网站 | 特级淫片欧美高清视频蜜桃 | 亚洲精品免费观看 | 亚洲国产精品久久精品成人网站 | 毛片区| 白丝捆绑vk| 国产精品午夜剧场 | 雪恋电影完整版免费观看 | 欧美精品一区二区在线观看 | 娇妻终于接受了3p的调教 | 99久久精品免费精品国产 | 波多野结衣被绝伦强在线观看 | 翁用力的抽插 | 草草国产成人免费视频 | 亚洲日日做天天做日日谢 | 欧美日韩国产成人综合在线影院 | 福利一区在线观看 | 国产专区日韩精品欧美色 | 扒开老师挠尿口到崩溃刑罚 | 亚洲欧美日韩中文字幕久久 | 处女摘花| 乌克兰成人性色生活片 | 亚洲精品色婷婷在线影院麻豆 | 美国xaxwaswaskino| 国产日韩欧美在线播放 | 青草青青在线视频 | 性bbbb妇女激情| 91精品国产91热久久p | 亚洲国产综合久久精品 | 亚洲成色 | 国产精品天天影视久久综合网 | 吃瓜视频在线观看 | 亚洲国产99在线精品一区69堂 | 97精品久久天干天天蜜 | 色综合综合 | 欧美一级在线播放 | 国产成人免费a在线资源 |