围绕事务服务的Spring AOP

时间:2019-07-18 03:36:15

标签: spring-aop spring-transactions javers

我有2个表CONTRACTLG_CONTRACT。每次合同更新时,我都希望将更改存储在LG_CONTRACT中。

我的AOP

    @Around(value = "execution(* id.co.somepackage.cofinjms.service.implement.ConverterImpl.signContract(id.co.somepackage.cofinjms.entity.Contract,net.somepackage.homerselect.contract.v3.ContractFullInfoRequest))")
    public Contract around(ProceedingJoinPoint joinPoint) throws Throwable {
        Contract contract = (Contract) joinPoint.getArgs()[0];
        ContractFullInfoRequest contractFullInfoRequest = (ContractFullInfoRequest) joinPoint.getArgs()[1];
        if (contract != null) {
            javers.commit(contract.getContractNumber(), contract);
        }
        Contract updatedContract = (Contract) joinPoint.proceed();
        if (contract != null) {
            javers.commit(contract.getContractNumber(), updatedContract);
            Set<LgContract> lgContractSet = new HashSet<>();
            Date date = new Date();
            List<Change> changes = javers.findChanges(QueryBuilder.byInstanceId(contract.getContractNumber(), Contract.class).build());
            for (Change change : changes) {
                if (change.getClass().equals(ValueChange.class)) {
                    ValueChange vc = (ValueChange) change;
                    String oldValue = vc.getLeft() == null ? null : vc.getLeft().toString();
                    LgContract lgContract = new LgContract();
                    lgContract.setContract(contract);
                    lgContract.setColumnName(vc.getPropertyName());
                    lgContract.setValueOld(oldValue);
                    lgContract.setValueNew(vc.getRight() != null ? vc.getRight().toString() : null);
                    lgContract.setDtimeCreated(date);
                    lgContract.setSystemEvent(contractFullInfoRequest.getSystemEvent().toString());
                    lgContractSet.add(lgContract);
                }
            }
            updatedContract.setLgContracts(lgContractSet);
        }
        return updatedContract;
    }

服务

@Transactional
    public Contract saveContract(String message) throws ContractNotFoundException {
        ContractFullInfoRequest contractFullInfoRequest = JAXB.unmarshal(new StringReader(message), ContractFullInfoRequest.class);
        Contract contract = contractFullInfoRepository.findOne(contractFullInfoRequest.getData().getContractCode());
        switch (contractFullInfoRequest.getSystemEvent()) {
            case CONTRACT_SIGN_SE:
                contract = converter.signContract(contract, contractFullInfoRequest);
                break;
            case CONTRACT_ACTIVATION_SE:
                if (contract != null)
                    contract = converter.logBslStatusChange(contract, contractFullInfoRequest);
                else
                    contract = converter.activateNewContract(contractFullInfoRequest);
                break;
            case CONTRACT_PAID_OFF_SE:
            case CONTRACT_FINISHING_AUTOMATICALLY_SE:
            case CONTRACT_CANCELLATION_SE:
            case CONTRACT_FER_EXECUTED_SE:
            case PARTIAL_EARLY_REPAYMENT_SE:
                if (contract != null)
                    converter.logBslStatusChange(contract, contractFullInfoRequest);
                else
                    throw new ContractNotFoundException("Contract In MsContract Not Found");
                break;
            case CONTRACT_SERVICE_CHANGE_SE:
                if (contract != null)
                    converter.convertServiceChange(contract, contractFullInfoRequest);
                else
                    throw new ContractNotFoundException("Contract In MsContract Not Found");
                break;
            default:
                throw new NotYetImplementedException();
        }
        return contract;
    }

AOP的工作原理如下

  1. 获取现有合同(在进行更新/签名之前)
  2. 提交现有合同
  3. 让signContract服务继续进行,并将更新后的合同存储在CONTRACT表中
  4. 比较签约过程前后的合同,然后将更改存储在LG_Contract表中

在大多数情况下,这是可行的,但是我注意到在某些情况下,AOP能够存储更改,但是合同本身并未更新。

这是一切正常运行时LG_CONTRACT的外观(状态从S> N> A更改)

3900742668  bslStatus   S   N   06-MAR-19 10.17.07.967000000 AM CONTRACT_SIGN_SE
3900742668  bslStatus   N   A   07-MAR-19 06.04.24.963000000 PM ContractActivationSE

以及签名过程无法按预期进行时(状态从未在CONTRACT表中设置为N,但是LG_CONTRACT知道何时将状态设置为N)

3902643905  bslStatus   S   N   11-JUL-19 11.32.09.400000000 AM CONTRACT_SIGN_SE
3902643905  bslStatus   S   A   12-JUL-19 10.51.00.383000000 AM ContractActivationSE

我不明白为什么有时 AOP能够将合同状态从S更改为N的情况存储在LG_CONTRACT中,但不能反映在CONTRACT表中。

>

0 个答案:

没有答案