我试图学习如何使用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>
的IndexController
执行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);
}
}
DAO
@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>
答案 0 :(得分: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>