星空网 > 软件开发 > ASP.net

Spring事务管理的三种方式

一 、第一种:全注解声明式事务 1.<?"1.0" encoding="UTF-8"?> 2.<beans "http://www.springframework.org/schema/beans" 3.    "http://www.w3.org/2001/" 4.    "http://www.springframework.org/schema/p" 5.    "http://www.springframework.org/schema/tx" 6.    "http://www.springframework.org/schema/aop" 7.    "http://www.springframework.org/schema/context" 8.    xsi:schemaLocation="http://www.springframework.org/schema/beans 9.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10.    http://www.springframework.org/schema/tx 11.    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12.    http://www.springframework.org/schema/aop 13.    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 14.    http://www.springframework.org/schema/context 15.    http://www.springframework.org/schema/context/spring-context-2.5.xsd" 16.    default-init-method="init">   17.    18.  <!-- 引入jdbc配置文件 --> 19.  <context:property-placeholder location="classpath:jdbc.properties" /> 20.   21.  <!--创建jdbc数据源 --> 22.  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 23.    <property name="driverClassName" value="${driver}" /> 24.    <property name="url" value="${url}" /> 25.    <property name="username" value="${username}" /> 26.    <property name="password" value="${password}" /> 27.  </bean> 28.  29.  <!-- 创建SqlSessionFactory,同时指定数据源 --> 30.  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31.    <property name="dataSource" ref="dataSource" /> 32.    <!-- 配置sql映射文件所在位置 注意:默认与mapper类位置相同  --> 33.    <property name="mapperLocations" value="classpath:sqlmap/*." /> 34.  </bean> 35.  36.  <!-- 配置事务管理器:第一种 全注解声明式事务 --> 37.  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 38.    <property name="dataSource" ref="dataSource" /> 39.  </bean> 40.  <tx:annotation-driven transaction-manager="transactionManager" /> 41.  <!-- 第一种 结束位置 --> 42.  <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper --> 43.  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 44.    <property name="basePackage" value="com.xieke.test.mapper" /> 45.    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  46.    <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->  47.  </bean> 48.   49.</beans>   使用时:在需要事务控制的类上加上@Transactional注解就可以了.  二、第二种:使用tx标签配置的**声明式事务1.<?"1.0" encoding="UTF-8"?>  2.<beans "http://www.springframework.org/schema/beans"  3.    "http://www.w3.org/2001/"  4.    "http://www.springframework.org/schema/p"  5.    "http://www.springframework.org/schema/tx"  6.    "http://www.springframework.org/schema/aop"  7.    "http://www.springframework.org/schema/context"  8.    xsi:schemaLocation="http://www.springframework.org/schema/beans  9.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  10.    http://www.springframework.org/schema/tx  11.    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  12.    http://www.springframework.org/schema/aop  13.    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  14.    http://www.springframework.org/schema/context  15.    http://www.springframework.org/schema/context/spring-context-2.5.xsd"  16.    default-init-method="init">    17.     18.  <!-- 引入jdbc配置文件 -->  19.  <context:property-placeholder location="classpath:jdbc.properties" />  20.    21.  <!--创建jdbc数据源 -->  22.  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  23.    <property name="driverClassName" value="${driver}" />  24.    <property name="url" value="${url}" />  25.    <property name="username" value="${username}" />  26.    <property name="password" value="${password}" />  27.  </bean>  28.   29.  <!-- 创建SqlSessionFactory,同时指定数据源 -->  30.  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  31.    <property name="dataSource" ref="dataSource" />  32.    <!-- 配置sql映射文件所在位置 注意:默认与mapper类位置相同  -->  33.    <property name="mapperLocations" value="classpath:sqlmap/*." />  34.  </bean>  35.    36.  <!-- 配置事务管理器:第二种 使用tx标签配置的**声明式事务 -->  37.  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  38.    <property name="dataSource" ref="dataSource" />  39.  </bean>  40.  <!-- 配置事务的传播特性 -->  41.  <tx:advice id="txAdvice" transaction-manager="transactionManager">  42.    <tx:attributes>  43.      <tx:method name="add*" propagation="NESTED" />  44.      <tx:method name="del*" propagation="NESTED" />  45.      <tx:method name="*" read-only="true" />  46.    </tx:attributes>  47.  </tx:advice>  48.  <!-- 配置事务的切入点 -->  49.  <aop:config>  50.    <aop:pointcut id="targetMethod" expression="execution(* com.xieke.test.service.impl.*.*(..))" />  51.    <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" />  52.  </aop:config>  53.  <!-- 第二种 结束结束位置 -->  54.   55.  <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  56.  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  57.    <property name="basePackage" value="com.xieke.test.mapper" />  58.    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />   59.    <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->   60.  </bean>  61.    62.</beans>    三、 第三种:使用**声明式事务 1.<?"1.0" encoding="UTF-8"?> 2.<beans "http://www.springframework.org/schema/beans" 3.    "http://www.w3.org/2001/" 4.    "http://www.springframework.org/schema/p" 5.    "http://www.springframework.org/schema/tx" 6.    "http://www.springframework.org/schema/aop" 7.    "http://www.springframework.org/schema/context" 8.    xsi:schemaLocation="http://www.springframework.org/schema/beans 9.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10.    http://www.springframework.org/schema/tx 11.    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12.    http://www.springframework.org/schema/aop 13.    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 14.    http://www.springframework.org/schema/context 15.    http://www.springframework.org/schema/context/spring-context-2.5.xsd" 16.    default-init-method="init">   17.    18.  <!-- 引入jdbc配置文件 --> 19.  <context:property-placeholder location="classpath:jdbc.properties" /> 20.   21.  <!--创建jdbc数据源 --> 22.  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 23.    <property name="driverClassName" value="${driver}" /> 24.    <property name="url" value="${url}" /> 25.    <property name="username" value="${username}" /> 26.    <property name="password" value="${password}" /> 27.  </bean> 28.  29.  <!-- 创建SqlSessionFactory,同时指定数据源 --> 30.  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31.    <property name="dataSource" ref="dataSource" /> 32.    <!-- 配置sql映射文件所在位置 注意:默认与mapper类位置相同  --> 33.    <property name="mapperLocations" value="classpath:sqlmap/*." /> 34.  </bean> 35.   36.  <!-- 配置事务管理器:第三种 使用**声明式事务 -->   37.  <bean id="transactionManager"  38.    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  39.    <property name="dataSource" ref="dataSource" />  40.  </bean>   41.  <bean id="transactionInterceptor"   42.    class="org.springframework.transaction.interceptor.TransactionInterceptor">   43.    <property name="transactionManager" ref="transactionManager" /> 44.    <!-- 配置事务属性  --> 45.    <property name="transactionAttributes">   46.      <props> 47.        <!-- PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,则新建一个事务  --> 48.        <prop key="add*">PROPAGATION_REQUIRED</prop> 49.        <prop key="del*">PROPAGATION_REQUIRED</prop>   50.      </props>   51.    </property>   52.  </bean>  53.  <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   54.    <property name="beanNames">   55.      <list>   56.        <value>*ServiceImpl</value>  57.      </list>   58.    </property>   59.    <property name="interceptorNames">   60.      <list>   61.        <value>transactionInterceptor</value>   62.      </list>   63.    </property>   64.  </bean>   65.  <!-- 第三种 结束位置 --> 66.  67.  <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper --> 68.  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 69.    <property name="basePackage" value="com.xieke.test.mapper" /> 70.    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  71.    <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->  72.  </bean> 73.   74.</beans> 

Spring事务的传播属性:
下载地址   
PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS -- 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY -- 支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW -- 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED -- 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER -- 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED -- 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。




原标题:Spring事务管理的三种方式

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Naqel纳客国际快递:https://www.goluckyvip.com/news/4799.html
2018年亚马逊FBA配送费大调整,卖家如何应对?:https://www.goluckyvip.com/news/48.html
海关总署:关于2019年关税调整方案的公告:https://www.goluckyvip.com/news/480.html
亚马逊抽检,您的饰品类产品做了REACH认证了吗?:https://www.goluckyvip.com/news/4800.html
2021东南亚Shopee真的好做吗?:https://www.goluckyvip.com/news/4801.html
5400万播放!TikTok“泳池带娃神器”成为七月婴儿泳池爆款商品:https://www.goluckyvip.com/news/4802.html
皇帝的皇宫=:https://www.vstour.cn/a/363188.html
海南岛琼海市旅游景点 琼海市的旅游景点:https://www.vstour.cn/a/363189.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流