我在Spring上使用mybatis。我有一个执行删除然后插入操作的API。我希望这些操作在一个事务中,以便在插入失败时回滚删除操作。
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@MapperScan(basePackages = "com.abc..mapper")
public class ABCCoreDataConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
....
.....
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver()
.getResources("classpath*:com/abc//mapper/**/*.xml"));
return sessionFactory;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
服务类:
@Service
@Transactional
public class MyService implements IMyService{
@Autowired
private MyMapper myMapper;
@Transactional(propagation = Propagation.REQUIRED)
public boolean addRecord() throws Exception {
myMapper.deleteRecord(id);
myMapper.insertRecord(- - -);
return true;
}
}
即使插入因MySQLIntegrityConstraintViolationException而失败,仍然不会恢复删除操作。
注意:删除和插入是映射器级别的两种不同方法。因此,我们在服务层使用了交易。
堆栈跟踪:
23:11:29.452 DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
23:11:29.458 DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d40dbdb]
23:11:36.685 DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [jdbc:mysql://127.0.0.1:13306/abc?connectTimeout=1000, UserName=xyz@10.0.0.0, MySQL Connector Java] will be managed by Spring
23:11:36.690 DEBUG MyDao.delete - ==> Preparing: DELETE FROM .....
23:11:36.711 DEBUG MyDao.delete - ==> Parameters: ....
23:11:36.807 DEBUG MyDao.delete - <== Updates: 2
23:11:36.807 DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d40dbdb]
23:11:36.807 DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d40dbdb] from current transaction
23:11:36.832 DEBUG MyDao.insert - ==> Preparing: INSERT INTO .....
23:11:36.833 DEBUG MyDao.insert - ==> Parameters: ...
23:11:36.927 DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d40dbdb]
23:11:36.997 DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d40dbdb]
23:11:36.998 DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d40dbdb]
答案 0 :(得分:0)
不确定为什么要删除记录,如果可能,重新插入,更新将是更好的选择。这更多地涉及交易的概念,这是一个想法。您可以在mapper类中执行以下操作。
@Transactional(propagation = Propagation.REQUIRED)
public boolean deleteRecord() throws Exception {
// delete code here
return true;
}
@Transactional(propagation = Propagation.REQUIRED)
public boolean addRecord() throws Exception {
// add code here
return true;
}
}
此处spring注释将处理您的事务。有关更多信息,实际事务使用此处说明的代理概念 Spring - @Transactional - What happens in background?