好的,对于已经提到的技术,这是一个非常奇怪的行为,我有一个调用服务的控制器,这调用了一个 dao @Column nullable = false, unique = true
,当我对 dao 或服务并插入重复的值会引发异常
org.springframework.dao.DataIntegrityViolationException
,这没关系,是预期的行为。但是当我运行网络应用程序时,在服务完成后抛出异常,它在执行 dao 时没有执行。所以这迫使我在控制器中捕获异常,而不是在服务中。
- 控制器//开始
- 服务 //继续
- DAO //继续(但此时应该抛出异常)
- service // 完成(一个 try catch 围绕着 dao,但没有抛出异常)。
- 控制器//抛出异常。
我在每种方法中添加了一些日志,所以我可以看到
插入 ...
服务调用完成后执行语句。
谢谢你。
编辑
这是 xml 事务定义。
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="crea*" propagation="REQUIRED"/>
<tx:method name="obtener*" propagation="SUPPORTS" read-only="true" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.company.service..*.*(..))"
id="txPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
这是两段代码,第一段是捕获异常。
package com.company.service.tarea.ventas.impl
public Producto obtenerSiExisteDuplicadaClave(String clave) {
try{
return productoService.buscarPorClave(clave);
}catch(EmptyResultDataAccessException e){
log.error(e.getMensaje());
}
return null;
}
此代码不起作用
com.company.service.tarea.almacenpt.impl
public void creaEntradaProduccion(Entrada entrada, BindingResult bindingResult) {
if(log.isDebugEnabled())log.debug("creaEntradaProduccion");
entradaService.peristeEntrada(entrada);
if(log.isDebugEnabled())log.debug("persistido...");
}
异常被抛出,直到代码执行并将执行返回给控制器,它到达日志“persistido”。