【发布时间】:2012-03-28 20:31:51
【问题描述】:
目前我在我的应用程序中使用 spring 声明式事务管理器。在数据库操作期间,如果违反任何约束,我想检查数据库的错误代码。我的意思是我想在异常发生后运行一个选择查询。所以我在我的 Catch 块中捕获了 DataIntegrityViolationException,然后我试图再执行一个错误代码查询。但是那个查询没有被执行。我假设因为我正在使用事务管理器,如果发生任何异常,下一个查询不会被执行。那正确吗?。我想在将结果返回给客户端之前执行该错误代码查询。有什么办法吗?
@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());
}
}
请帮帮我。
【问题讨论】:
标签: spring transactions