关于Spring Transaction Manager

时间:2012-03-27 12:20:47

标签: spring transactions

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


    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

我认为您所描述的问题与交易管理无关。如果您DataIntegrityViolationException块中发生try(),则catch()内的代码应执行。也许与[{1}}发生异常或您的DataIntegrityViolationException抛出另一个异常。通常,只有从方法调用返回后才会应用事务逻辑,直到那时你可以使用普通的Java语言结构做任何你喜欢的事情。我建议你在你的错误错误处理程序或一些调试语句中放置断点,看看实际发生了什么。

相关问题