目前我在我的应用程序中使用spring声明式事务管理器。在数据库操作期间,如果违反任何约束,我想检查数据库的错误代码。我的意思是我想在异常发生后运行一个选择查询。所以我捕捉我的Catch块内的DataIntegrityViolationException,然后我试图执行一个更多的错误代码查询。但是那个查询没有被执行。我假设,因为我使用事务管理器,如果发生任何异常下一个查询没有得到执行。是对的吗?。我想在将结果返回给客户端之前执行该错误代码查询。任何方式来做到这一点?关于Spring Transaction Manager
@Override
@Transactional
public LineOfBusinessResponse create(
CreateLineOfBusiness createLineOfBusiness)
throws GenericUpcException {
logger.info("Start of createLineOfBusinessEntity()");
LineOfBusinessEntity lineOfBusinessEntity =
setLineOfBusinessEntityProperties(createLineOfBusiness);
try {
lineOfBusinessDao.create(lineOfBusinessEntity);
return setUpcLineOfBusinessResponseProperties(lineOfBusinessEntity);
}
// Some db constraints is failed
catch (DataIntegrityViolationException dav) {
String errorMessage =
errorCodesBd.findErrorCodeByErrorMessage(dav.getMessage());
throw new GenericUpcException(errorMessage);
}
// General Exceptions handling
catch (Exception exc) {
logger.debug("<<<<Coming inside General >>>>");
System.out.print("<<<<Coming inside General >>>>");
throw new GenericUpcException(exc.getMessage());
}
}
public String findErrorCodeByErrorMessage(String errorMessage)throws GenericUpcException {
try{
int first=errorMessage.indexOf("[",errorMessage.indexOf("constraint"));
int last=errorMessage.indexOf("]",first);
String errorCode=errorMessage.substring(first+1, last);
//return errorCodesDao.find(errorCode);
return errorCode;
}
catch(Exception e)
{
throw new GenericUpcException(e.getMessage());
}
}
请帮帮我。
感谢您的回复。如果我使用@transaction注解它甚至不会在异常发生后触发catch块。它直接从数据库中给出错误字符串。它不会让我发现错误。如果我不使用事务注释,一切工作正常。而且我确信在我的findErrorCodeByErrorMessage()方法中没有问题。无论如何,我发布了我的findErrorCodeByErrorMessage()方法code.Please帮助我。 – Dilip 2012-03-28 12:25:11