propagation="REQUIRED"和PROPAGATION="REQUIRES_NEW"区别

 
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203
(官方)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);
}