propagation="REQUIRED"和PROPAGATION="REQUIRES_NEW"區(qū)別

 
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203
(官方)PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。
PROPAGATION_REQUIRES_NEW--新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。

馬克-to-win:當(dāng)兩個(gè)不同的pointcut之間涉及調(diào)用方法時(shí),就涉及到事務(wù)傳播屬性。比如上面的方法updateRegister,如果它的屬性是PROPAGATION_REQUIRED,則它會(huì)和調(diào)用它的方法login同屬一個(gè)事務(wù),如果是REQUIRES_NEW,則在不同的事務(wù)。當(dāng) updateRegister已經(jīng)提交之后updateRegisterWrong發(fā)生錯(cuò)誤的時(shí)候,如果是REQUIRES_NEW,則 updateRegister不會(huì)回滾,而如果是REQUIRED,則會(huì)回滾。注意下例,我們把兩個(gè)方法故意放在兩個(gè)pointcut里??匆幌鲁绦虬?REQUIRED變成REQUIRES_NEW以后的區(qū)別。



例 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);
}