0

嗨,我有 spring 服务方法(A 方法),它将执行两个操作。首先它将通过 hibernate dao 方法(B 方法)将数据保存到数据库中。然后它将向 spring-activiti 工作流(C 方法)发出信号以移动到下一阶段。现在我正在使用弹簧事务管理器。我正在使用如下事务。所以这里我的问题是,如果在工作流方法中捕获到一些异常,那么数据库更改不应该被恢复..

 @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
@Override
public WorkflowResponseBO saveDraft(PETIncidentBO pETIncident) throws DDMApplicationException {
Investigation investigation = savePETIncident(pETIncident);
WorkflowResponseBO workflowResponse = null;
if (pETIncident.getWorkflowId() == null) {
    workflowResponse = initiateWorkflow(pETIncident, investigation);
}
return workflowResponse;
}

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = Exception.class)
private Investigation savePETIncident(PETIncidentBO pETIncident) throws DDMApplicationException {
LOGGER.info("Entered Method :: savePETIncident");
Investigation investigation = null;
try {
    if (pETIncident.getWorkflowId() != null) {
    investigation = investigationDao.findInvestigationByWorkflowId(pETIncident.getWorkflowId());
    }

    // Incident Data
    Incident incident = getIncidentDetails(investigation, pETIncident);

    // Investigation Data
    if (investigation == null) {
    investigation = new Investigation();
    investigation.setInvestigatedInvolvedPerson(getIncidentInvolvedPersonDetails(incident, pETIncident));
    MasterLookup allegedViolationType = null;
    allegedViolationType = masterLookupDao.findMasterLookupByShortDesc(DDMConstants.PROBATIONARY_TERMINATION_LOOKUP);
    investigation.setAllegedViolationType(allegedViolationType);
    CollectiveBargainingAgreement collectiveBargainingAgreement = null;
    collectiveBargainingAgreement = collectiveBargainingAgreementDao.findCollectiveBargainingAgreementbyId(pETIncident.getCba().getCollectiveBargainingAgreementId());
    investigation.setCollectiveBargainingAgreement(collectiveBargainingAgreement);
    investigation.setIncident(incident);
    }
    // updating the address and rehire status of the involved person
    investigation = updateInvesigationInvolvedPersonDetails(investigation, pETIncident);

    InvestigationUnstructuredContent content = getInvestigationUnstructuredContentDetails(investigation, pETIncident);

    content = getInvestigationUnstContentDeliveryDetails(pETIncident, content, investigation.getInvestigatedInvolvedPerson().getPerson());

    investigation.getInvestigationUnstConts().add(content);

    investigation = getInvestigationAssignedPersonDetails(investigation, incident, pETIncident);

    if (investigation.getInvestigationId() == null) {
    investigationDao.createinvestigation(investigation);
    } else {
    investigationDao.updateinvestigation(investigation);
    }

} catch (DDMDBException de) {
    LOGGER.error(de);
    DDMServicesExceptionHandler.handleException(de);
} catch (DDMApplicationException da) {
    LOGGER.error(da);
    DDMServicesExceptionHandler.handleException(da);
} catch (Exception e) {
    LOGGER.error(e);
    DDMServicesExceptionHandler.handleException(e);
}
LOGGER.info("Exited Method :: savePETIncident");
return investigation;
}
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = Exception.class)
private WorkflowResponseBO initiateWorkflow(PETIncidentBO pETIncident, Investigation investigation) throws DDMApplicationException {
// updating the workflow id in the investigation table
WorkflowResponseBO workflowResponse = null;
PETWorkflowDetailsBO petWorkflowDetails = new PETWorkflowDetailsBO();
petWorkflowDetails.setCbaId(investigation.getCollectiveBargainingAgreement().getCollectiveBargainingAgreementId().toString());
petWorkflowDetails.setInitiatorEmployeeId(pETIncident.getIncidentCreatorEmployeeId());
petWorkflowDetails.setInvovledPersonEmployeeId(pETIncident.getInvolvedPersonDeliveryDetails().getPerson().getEmployeeId());
petWorkflowDetails.setReasonForTermination(pETIncident.getReasonForTermination().getLookupShortDesc());
petWorkflowDetails.setTerminationDate(DdmDateUtils.dateToString(pETIncident.getTerminationDate(), DDMConstants.DISPLAY_DATE_FORMAT));
workflowResponse = petWorkflowService.reportProbationaryEmployeeTermination(petWorkflowDetails);
investigation.setWorkflowId(workflowResponse.getProcessId());
try {
    investigationDao.updateinvestigation(investigation);
} catch (DDMDBException e) {
    LOGGER.error("Error while updating investigation table");
    DDMServicesExceptionHandler.handleException(e);

}
if (workflowResponse == null) {
    workflowResponse = new WorkflowResponseBO();
    workflowResponse.setProcessId(pETIncident.getWorkflowId());
    workflowResponse.setTaskId(pETIncident.getTaskId());
}
return workflowResponse;
}   
4

1 回答 1

0

当基于类的代理弹簧加载类并将 saveDraft 方法包装在事务中时。由于 spring 现在在处理对此类中方法的任何进一步调用时不在循环中,因此它不会创建嵌套事务。

如果您希望自调用也与事务一起包装,请考虑使用 AspectJ 模式。在这种情况下,首先不会有代理;相反,目标类将被编织(即,其字节码将被修改),以便将@Transactional 转换为任何类型的方法的运行时行为。

或者您可以使用程序化交易。

于 2013-06-06T22:09:36.870 回答