0

我正在尝试学习如何使用 AOP,并且我正在尝试在 Spring 的应用程序上下文中设置一个只读事务,但它不起作用,它仍然将数据提交到数据库。

我真的不知道我做错了什么,如果你能帮助我,我会很高兴。

应用程序上下文.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>

索引控制器

执行方法的请求代码:

@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>
4

1 回答 1

0

从这里可以看出(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>
于 2015-07-18T19:49:23.773 回答