Spring事务当中propagation=“REQUIRED“和PROPAGATION=“REQUIRES_NEW“的区别

3.propagation="REQUIRED"和PROPAGATION="REQUIRES_NEW"的区别  

(官方)PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。

马克-to-win:当两个不同的pointcut之间涉及调用方法时,就涉及到事务传播属性。比如上面的方法updateRegister,如果它的属性是PROPAGATION_REQUIRED,则它会和调用它的方法login同属一个事务,如果是REQUIRES_NEW,则在不同的事务。当updateRegister已经提交之后updateRegisterWrong发生错误的时候,如果是REQUIRES_NEW,则updateRegister不会回滚,而如果是REQUIRED,则会回滚。注意下例,我们把两个方法故意放在两个pointcut里。看一下程序把REQUIRED变成REQUIRES_NEW以后的区别。

例 2.3.1

package service;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.INiutDAO;
import service.interfac.ILoginService;

public class LoginServiceImpl implements ILoginService {
    @Resource
    private JdbcTemplate jt;
    private INiutDAO iNiutDAO;
    public void setINiutDAO(INiutDAO iNiutDAO) {
        this.iNiutDAO = iNiutDAO;
    }
    public void login() {
        iNiutDAO.daoUpdateRegister(13,1);
        System.out.println("successfully update 1");
        updateRegisterWrong(19,2);
        System.out.println("successfully update 2");
    }    
    public void updateRegisterWrong(int age,int id) {
        String sql = "UPDATE register SET age=? WWWWWWW id=?";
        Object params[] = new Object[] { new Integer(age),
                new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
}



<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
       >
 
    <context:component-scan
        base-package="com" />
    <context:component-scan
        base-package="service" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
 <bean id="NiutDAO" class="com.NiutDAO">
    </bean>
    <bean id="loginService" class="service.LoginServiceImpl" >
        <property name="iNiutDAO">
            <ref bean="NiutDAO" />
        </property>
    </bean>
   
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="login*" propagation="REQUIRED" />
            <tx:method name="daoUpdateRegister" propagation="REQUIRED" />        
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 这种写法也正确"execution(* service.*.*(..))"-->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />
        <aop:pointcut id="myDaoPointcut" expression="execution(* com.NiutDAO.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myDaoPointcut"/>        
    </aop:config> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
         <property name="driverClassName"
                      value="com.mysql.jdbc.Driver"></property>
         <property name="url" 
                      value="jdbc:mysql://localhost:3306/test"></property>
         <property name="username"
                      value="root"></property>
         <property name="password" 
                      value="1234"></property>
    </bean>
    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>



package com;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
public class NiutDAO implements INiutDAO {
    @Resource
    private JdbcTemplate jt;
    public void daoUpdateRegister(int age,int id) {
        String sql = "UPDATE register SET age=? WHERE id=?";
        Object params[] = new Object[] { new Integer(age),
                 new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
}


package com;
public interface INiutDAO {
    public void daoUpdateRegister(int age,int id);
}

更多请见下节:http://www.mark-to-win.com/tutorial/frame_Spring_PROPAGATIONREQUIRES_NEW.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值