2012-03-27 44 views
0

目前我在我的应用程序中使用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()); 
     } 


    } 

请帮帮我。

回答

0

我不认为你描述的问题与事务管理有关。如果DataIntegrityViolationException发生在您的try()区块内,您应该执行catch()内的代码。或许与DataIntegrityViolationException不同的异常发生或您的findErrorCodeByErrorMessage()引发另一个异常。一般而言,事务逻辑只会在您从方法调用中返回时才应用,直到您可以使用普通的Java语言构造执行任何您喜欢的操作为止。我建议你在你的错误错误处理程序或一些调试语句中加入断点来查看实际发生的事情。

+0

感谢您的回复。如果我使用@transaction注解它甚至不会在异常发生后触发catch块。它直接从数据库中给出错误字符串。它不会让我发现错误。如果我不使用事务注释,一切工作正常。而且我确信在我的findErrorCodeByErrorMessage()方法中没有问题。无论如何,我发布了我的findErrorCodeByErrorMessage()方法code.Please帮助我。 – Dilip 2012-03-28 12:25:11

相关问题