【发布时间】:2018-07-15 19:31:39
【问题描述】:
下面是我的 applicationContext.xml 代码和我的 serviceImpl 类。
我已将 @service 和 @Transactional 添加到我的代码中。
但事务不会因异常而回滚。即使 table2 插入失败,它也会插入表 1。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<!-- Persistence layer -->
<context:annotation-config />
<context:component-scan base-package="com.tax.data" />
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="oracleds" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oracleds" />
<property name="configLocation" value="classpath:config/myBatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tax.data.persistence.mapper" />
</bean>
<bean id="oracleds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${ORACLE_DB_URL}" />
<property name="username" value="${ORACLE_DB_USER}" />
<property name="password" value="${ORACLE_DB_PWD}" />
</bean>
</beans>
服务类伪
@Service
public class EditTaxServiceImpl implements taxService{
private static final Logger LOG = LogManager.getLogger(EditTaxServiceImpl.class);
@Autowired
private TaxMapper taxMapper;
//..//
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = DataException.class)
public void insertUserTaxRequest(UserRequest userRequest, TaxProcess taxProcess) throws DataException{
UserInfo userInfo = new UserInfo();
InterfaceInfo interfaceInfo = new InterfaceInfo();
TaxInfo taxInfo = new TaxInfo();
try{
///..///
taxMapper.insertUserInfo(userInfo);
taxMapper.insertTaxInfo(taxInfo);
taxMapper.insertTaxEditInfo(taxInfo);
} catch (Exception ex) {
throw new DataException("EditTaxServiceImpl Service : Error in inserting EditTaxServiceImpl"+ ex.getMessage());
}
}
}
异常类
package com.tax.data.exception;
public class DataException extends Exception {
private static final long serialVersionUID = 1553804688073838262L;
public DataException( String message) {
super(message);
}
}
【问题讨论】:
-
我认为您需要将
datasource添加到txManager,类似这样的<property name="dataSource" ref="oracleds"/> -
你为什么使用
JtaTransactionManager?您正在使用不是 XA 能力的DriverManagerDataSource。你也有一个资源,为什么要使用它?请改用DataSourceTransactionManager并将数据源注入其中。 -
@M. Deinum 我已经添加了 DataSourceTransactionManager 并更新了注入数据源的代码,但回滚仍然不起作用。
-
您正在捕获此异常并使用它而不是传播异常
-
你能发
DataException的代码吗?
标签: java spring spring-mvc spring-transactions