當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring声明式事务管理
生活随笔
收集整理的這篇文章主要介紹了
Spring声明式事务管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
事務管理方式
1.編碼方案 不建議使用,它具有侵入性。在原有的業務代碼基礎上去添加事務管理代碼
2. 聲明式事務控制,基于AOP對目標進行代理,添加around環繞通知。
這種方案,它不具有侵入性,不需要修改原來的業務代碼
基于xml配置聲明式事務管理方案
第一步:在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是傳統的spring advice
1. 聲明事務管理器
2.配置通知
Spring為我們提供了一個TransactionInterceptor來完成增強
對于這個增強,我們可以使用spring為我們提供的一個標簽< tx:advice>來完成操作
<!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必須的,對哪些方法進行事務控制isolation 可選 設置事務隔離級別 默認是DEFAULT propagation:可選 設置事務傳播 默認值 REQUIREDtimeout 可選 超時時間 默認值-1 read-only 可選 默認值是false 如果不是只讀,它可以對insert update delete操作,如果是只讀不可以。rollback-for 可選 可以設置一個異常,如果產生這個異常,觸發事務回滾no-rolback-for 可選 可以設置一個異常,如果產生這個異常,不會觸發事務回滾--><tx:method name="account" /></tx:attributes></tx:advice>3.配置切面
因為使用的是傳統的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" /><!-- 創建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"><!-- 當注入dataSource后,底層會自動創建一個JdbcTemplate --><property name="dataSource" ref="c3p0DataSource" /></bean><!-- 配置事務管理器 --><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:必須的,對哪些方法進行事務控制isolation 可選 設置事務隔離級別 默認是DEFAULT propagation:可選 設置事務傳播 默認值 REQUIREDtimeout 可選 超時時間 默認值-1 read-only 可選 默認值是false 如果不是只讀,它可以對insert update delete操作,如果是只讀不可以。rollback-for 可選 可以設置一個異常,如果產生這個異常,觸發事務回滾no-rolback-for 可選 可以設置一個異常,如果產生這個異常,不會觸發事務回滾--><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><!--開啟事務管理注解驅動--><tx:annotation-driven transaction-manager="transactionManager"/> </beans>基于annotation聲明式事務管理方案
可以使用@Transaction來在類或方法上添加聲明式事務管理
注意:需要在applicationContext.xml文件中使用
相當于開啟注解事務控制
問題:關于xml方式與annotation方式的優缺點?
從簡單上來說,使用注解更方便。
使用配置的方案,可以對事務配置進行集中維護。
總結
以上是生活随笔為你收集整理的Spring声明式事务管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring JdbcTemplate
- 下一篇: AngularJS路由使用示例