Java spring中的申报事务不工作
我正在使用spring声明性事务特性。像这样的 spring配置的XML文件 服务层代码 但当我通过控制器发送callmakepayment方法时,它会将记录保存到登录表中,但当它移动到插入到支付表中时失败,因为我编写了查询以便它能够通过异常。我不明白为什么交易不起作用。因为@Transactional annotation在makePayment方法上,所以不应该在db中进行操作 请告诉我这个代码出了什么问题 您在makePayment方法中的SQL查询是:Java spring中的申报事务不工作,java,spring,spring-mvc,transactions,Java,Spring,Spring Mvc,Transactions,我正在使用spring声明性事务特性。像这样的 spring配置的XML文件 服务层代码 但当我通过控制器发送callmakepayment方法时,它会将记录保存到登录表中,但当它移动到插入到支付表中时失败,因为我编写了查询以便它能够通过异常。我不明白为什么交易不起作用。因为@Transactional annotation在makePayment方法上,所以不应该在db中进行操作 请告诉我这个代码出了什么问题 您在makePayment方法中的SQL查询是: insert into payme
insert into payment (id, name, amount) values('abc', 100)
您要求查询在数据库表中插入id、name和amount,但只提供2个值,即abc和100
如果表中的id列是autoincrement id,则无需在SQL查询中提及它。makePayment方法中的SQL查询应如下所示:
insert into payment (name, amount) values('abc', 100)
你到底想做什么我想遵循ACID属性,这意味着要么插入两条记录,要么不插入任何一条记录。所以你是说,即使commonDao.makePayment抛出异常,a行也会添加到登录表中?是的,但不应该发生。我仍在挣扎。有人能告诉我为什么会这样吗?我是故意这么做的。因此,由于此错误,事务失败,不应将任何记录插入表中。但记录被插入到登录表中。如果事务正在工作,则不应发生这种情况。
@Service
public class CommonServiceImpl implements CommonService {
@Autowired
private CommonDao commonDao;
// @Transactional I tried both of them one by one but not worked
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
@Override
public boolean makePayment() {
try{
commonDao.signUp(new Object());
commonDao.makePayment(new Object());
} catch(DataAccessException ex){
return false;
}
return true;
}
}
insert into payment (id, name, amount) values('abc', 100)
insert into payment (name, amount) values('abc', 100)