簡體   English   中英

只讀事務在我的AOP配置中不起作用

[英]read-only transaction doesn't work in my AOP config

我試圖學習如何使用AOP,並且試圖在Spring的應用程序上下文中設置一個只讀事務,但是它不起作用,它仍然將數據提交給DB。

我真的不知道我在做什么錯,如果您能幫助我,我會很高興。

applicationcontext.xml

<?xml version="1.0" encoding="windows-1252"?>
<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:tx=     "http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx`enter code here`
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy proxy-target-class="true" />

<!--advice-->
    <tx:advice  id="txAdvice" transaction-manager="txManager">
       <tx:attributes>            
            <tx:method name="eliminar*" read-only="true"  />
        </tx:attributes>
    </tx:advice>

<!-- Aspect -->
    <bean id="Aop" class="com.all.mymavenii.dao.TiposSolicitudDAO"  />
    <aop:config>
        <aop:pointcut id="point" 
                      expression="execution(* com.all.mymavenii.dao.TiposSolicitudDAO.eliminar(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
    </aop:config>

<!-- Data Base configuration  -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/config/basedatos/jdbc.properties" />
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${sifa.jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${sifa.jdbc.url}" />
        <property name="user" value="${sifa.jdbc.username}" />
        <property name="password" value="${sifa.jdbc.password}" />
        <property name="acquireIncrement" value="${sifa.c3p0.acquireIncrement}" />
        <property name="minPoolSize" value="${sifa.c3p0.minPoolSize}" />
        <property name="maxPoolSize" value="${sifa.c3p0.maxPoolSize}" />
        <property name="maxIdleTime" value="${sifa.c3p0.maxIdleTime}" />
    </bean>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <context:component-scan base-package="com.all.mymavenii.servicio">
        <context:include-filter expression="org.springframework.stereotype.Service" type="annotation" />
    </context:component-scan>

    <context:component-scan base-package="com.all.mymavenii.dao">
        <context:include-filter expression="org.springframework.stereotype.Repository"    type="annotation" />
    </context:component-scan>

</beans>

索引控制器

執行methot的請求的代碼:

@Controller
@RequestMapping("/")
public class IndexControlador {

    @Autowired
    private Servicio service;
/*
. . .
*/
   @RequestMapping(value = "/vista2", method = RequestMethod.POST)
    public String tipoSolicitudEliminar(@RequestParam("clv") String claveTipo) {
        service.eliminar(claveTipo);
        return "redirect:/vista2";
    }      
}

服務

@Service
public class Servicio {
    @Autowired 
    private TiposSolicitudDAO tiposSolicitudDAO;

    public void eliminar(String clave)
    {
        tiposSolicitudDAO.eliminar(clave);        
    }
}

@Repository("tiposSolicitudDAO")
public class TiposSolicitudDAO {

    private JdbcTemplate jdbcTemplate;
    private final String DELETE_SQL;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public TiposSolicitudDAO() {
        this.DELETE_SQL = "DELETE FROM sifa_tipos_solicitud WHERE  tiso_clave =? ";
    }

 public void eliminar(String claveTiposSolicitud) {
        jdbcTemplate.update(DELETE_SQL, new Object[]{claveTiposSolicitud});
    }
}

依存關系

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.0.5.RELEASE</version>
</dependency>        
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.1</version>
</dependency>        
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.1</version>
</dependency>

如此處所示( https://jira.spring.io/browse/SPR-8959 ),只讀屬性不會導致JPATransactionManager和MySQL在事務結束時自動回滾,這是預期的。很多(包括我)。 Spring開發人員似乎並不認為這是一個錯誤,也沒有修復它的計划。

如果您想始終在某些方法的末尾回滾事務(因此將其設為只讀),我的方法是擴展spring的TransactionInterceptor

方法commitTransactionAfterReturning負責事務提交。

protected void commitTransactionAfterReturning(TransactionInfo txInfo) {
    if (txInfo != null && txInfo.hasTransaction()) {
        if (logger.isTraceEnabled()) {
            logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() + "]");
        }
        txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
    }
}

您可以從最后一行看到默認情況下它僅提交事務。 因此,我們需要做的是用if語句替換它。

if (txInfo.getTransactionAttribute().isReadOnly()) {
    txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
} else {            
    txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
}

這樣,您當然不能再使用<tx:advice> 我們還需要自己配置Bean。 下面是一個示例:

<bean id="txAdvice" class="yourTransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="read*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
            </prop>
            <prop key="*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
        </props>
    </property>
</bean>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM