首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >支持多事务的Spring服务方法

支持多事务的Spring服务方法
EN

Stack Overflow用户
提问于 2013-06-06 22:07:26
回答 1查看 234关注 0票数 0

嗨,我有spring服务方法(A方法),它将做两个operations.first它将保存数据到数据库通过hibernate dao方法(B方法).then它将发出信号到spring-activiti工作流(C方法)移动到下一个stage.Now我正在使用spring transaction manager.and我正在使用事务作为below.so在这里我的问题是如果在工作流方法中捕获到一些异常,那么数据库更改不应该被恢复。

代码语言:javascript
复制
 @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;
}   
EN

回答 1

Stack Overflow用户

发布于 2013-06-07 06:09:37

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

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

或者,您可以使用程序化事务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16964269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档