javascript
Spring声明式事务管理
事務(wù)管理方式
1.編碼方案 不建議使用,它具有侵入性。在原有的業(yè)務(wù)代碼基礎(chǔ)上去添加事務(wù)管理代碼
2. 聲明式事務(wù)控制,基于AOP對(duì)目標(biāo)進(jìn)行代理,添加around環(huán)繞通知。
這種方案,它不具有侵入性,不需要修改原來(lái)的業(yè)務(wù)代碼
基于xml配置聲明式事務(wù)管理方案
第一步:在applicationContext.xml文件中添加aop與tx的名稱空間
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">第二步:在applicationContext.xml文件中配置
Spring提供的advice是傳統(tǒng)的spring advice
1. 聲明事務(wù)管理器
2.配置通知
Spring為我們提供了一個(gè)TransactionInterceptor來(lái)完成增強(qiáng)
對(duì)于這個(gè)增強(qiáng),我們可以使用spring為我們提供的一個(gè)標(biāo)簽< tx:advice>來(lái)完成操作
<!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必須的,對(duì)哪些方法進(jìn)行事務(wù)控制isolation 可選 設(shè)置事務(wù)隔離級(jí)別 默認(rèn)是DEFAULT propagation:可選 設(shè)置事務(wù)傳播 默認(rèn)值 REQUIREDtimeout 可選 超時(shí)時(shí)間 默認(rèn)值-1 read-only 可選 默認(rèn)值是false 如果不是只讀,它可以對(duì)insert update delete操作,如果是只讀不可以。rollback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,觸發(fā)事務(wù)回滾no-rolback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,不會(huì)觸發(fā)事務(wù)回滾--><tx:method name="account" /></tx:attributes></tx:advice>3.配置切面
因?yàn)槭褂玫氖莻鹘y(tǒng)的spring的advice,需要使用
完整的applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 引入外部的properties文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 創(chuàng)建c3p0連接 --><bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- service --><bean id="accountService" class="cn.nwtxxb.service.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!-- dao --><bean id="accountDao" class="cn.nwtxxb.dao.AccountDAOImpl"><!-- 當(dāng)注入dataSource后,底層會(huì)自動(dòng)創(chuàng)建一個(gè)JdbcTemplate --><property name="dataSource" ref="c3p0DataSource" /></bean><!-- 配置事務(wù)管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0DataSource"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必須的,對(duì)哪些方法進(jìn)行事務(wù)控制isolation 可選 設(shè)置事務(wù)隔離級(jí)別 默認(rèn)是DEFAULT propagation:可選 設(shè)置事務(wù)傳播 默認(rèn)值 REQUIREDtimeout 可選 超時(shí)時(shí)間 默認(rèn)值-1 read-only 可選 默認(rèn)值是false 如果不是只讀,它可以對(duì)insert update delete操作,如果是只讀不可以。rollback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,觸發(fā)事務(wù)回滾no-rolback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,不會(huì)觸發(fā)事務(wù)回滾--><tx:method name="account" /></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* cn.nwtxxb.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config><!--開啟事務(wù)管理注解驅(qū)動(dòng)--><tx:annotation-driven transaction-manager="transactionManager"/> </beans>基于annotation聲明式事務(wù)管理方案
可以使用@Transaction來(lái)在類或方法上添加聲明式事務(wù)管理
注意:需要在applicationContext.xml文件中使用
相當(dāng)于開啟注解事務(wù)控制
問(wèn)題:關(guān)于xml方式與annotation方式的優(yōu)缺點(diǎn)?
從簡(jiǎn)單上來(lái)說(shuō),使用注解更方便。
使用配置的方案,可以對(duì)事務(wù)配置進(jìn)行集中維護(hù)。
總結(jié)
以上是生活随笔為你收集整理的Spring声明式事务管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring JdbcTemplate
- 下一篇: AngularJS路由使用示例