注释 @Transactional 在 spring 中不起作用
Annotation @Transactional not work in spring
为了避免死锁,我想逐行更新一个 ID 列表。要回滚 spring 中的数据,我使用注释事务。但是当我测试事务时,当更新 sql 抛出异常时,它没有从之前更新的记录中回滚数据。这是我的代码:
@Service
public class YahooBudgetUpdateService {
@Autowired
private AccounttUpdateMapper mapper;
public void accountUpdate() throws Exception {
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
updateAcnt(ids);
}
@Transactional
public void updateAcnt(List<String> ids) throw Exception {
for(int i=0; i<ids.size; i++){
updateById(ids.get(i));
}
}
}
Mapper.java:
public interface AccountUpdateMapper {
int updateById(String id);
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="src.mapper.AccountUpdateMapper">
<update id="updateById">
UPDATE ad_cpnt
SET
updatedOn = now(),
lst_updtd_dt= now(),
where
id = #{id}
</update>
</mapper>
我尝试使用 @Transactional(rollbackFor= {Exception.class}) 但它也没有用。有谁知道原因吗?
这里是一切可行的东西
Service
public class YahooBudgetUpdateService {
@Autowired
private AccounttUpdateMapper mapper;
@Transactional <-----This belongs here as it will not work with internal calls as explained in comments
public void accountUpdate() throws Exception {
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
updateAcnt(ids);
}
public void updateAcnt(List<String> ids) throw Exception {
for(int i=0; i<ids.size; i++){
mapper.updateById(ids.get(i)); <-----here you must invoke it from mapper instance not another internal call
}
}
}
为了避免死锁,我想逐行更新一个 ID 列表。要回滚 spring 中的数据,我使用注释事务。但是当我测试事务时,当更新 sql 抛出异常时,它没有从之前更新的记录中回滚数据。这是我的代码:
@Service
public class YahooBudgetUpdateService {
@Autowired
private AccounttUpdateMapper mapper;
public void accountUpdate() throws Exception {
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
updateAcnt(ids);
}
@Transactional
public void updateAcnt(List<String> ids) throw Exception {
for(int i=0; i<ids.size; i++){
updateById(ids.get(i));
}
}
}
Mapper.java:
public interface AccountUpdateMapper {
int updateById(String id);
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="src.mapper.AccountUpdateMapper">
<update id="updateById">
UPDATE ad_cpnt
SET
updatedOn = now(),
lst_updtd_dt= now(),
where
id = #{id}
</update>
</mapper>
我尝试使用 @Transactional(rollbackFor= {Exception.class}) 但它也没有用。有谁知道原因吗?
这里是一切可行的东西
Service
public class YahooBudgetUpdateService {
@Autowired
private AccounttUpdateMapper mapper;
@Transactional <-----This belongs here as it will not work with internal calls as explained in comments
public void accountUpdate() throws Exception {
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
updateAcnt(ids);
}
public void updateAcnt(List<String> ids) throw Exception {
for(int i=0; i<ids.size; i++){
mapper.updateById(ids.get(i)); <-----here you must invoke it from mapper instance not another internal call
}
}
}