현재 내 응용 프로그램에서 Spring 선언적 트랜잭션 관리자를 사용하고 있습니다. DB 작업 중에 제약 조건이 위반되면 데이터베이스에 대한 오류 코드를 확인하려고합니다. 예외가 발생한 후에 하나의 select 쿼리를 실행하고 싶다는 뜻입니다. 그래서 내 catch 블록 안에 DataIntegrityViolationException catch하고 다음 하나 더 오류 코드 쿼리를 실행하려고합니다. 그러나 그 쿼리는 실행되지 않습니다. 어떤 예외가 발생했을 때 트랜잭션 관리자를 사용하고 있기 때문에 다음 쿼리가 실행되지 않는 것으로 가정합니다. 그게 맞습니까? 결과를 클라이언트에 반환하기 전에 오류 코드 쿼리를 실행하고 싶습니다. 이걸 할 수있는 방법은 없나요?Spring 트랜잭션 관리자 정보
@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() 메소드 코드를 게시했습니다. 제발 도와주세요. – Dilip