使用ScheduledExecutorService保存(Entites),我得到分离的实体传递给持久性错误

时间:2017-07-28 10:51:56

标签: java hibernate entity spring-data-jpa scheduledexecutorservice

我有一个非常奇怪的错误,我似乎无法理解。 我需要使用ScheduledExecutorService来传递我创建的调查实体以进行编辑,然后保存为新的实体。

public void executeScheduled(Survey eventObject, long interval) {
    HashMap<String, String> eventRRules = StringUtils.extractSerialDetails(eventObject.getvCalendarRRule());

    long delay = 10000;

    ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

    Runnable runnable = new Runnable() {
        private int counter = 1;
        private int executions = Integer.parseInt(eventRRules.get("COUNT"));
        Survey survey = eventObject;
        public void run() {
            String uid = eventObject.getUniqueEventId();
            logger.info("SurveyController - executeScheduled - Iteration: " + counter);
            String serialUid = null;
            if (counter == 1) {
                serialUid = uid + "-" + counter;
            } else {
                serialUid = StringUtils.removeLastAndConcatVar(eventObject.getUniqueEventId(), Integer.toString(counter));
            }
            if (++counter > executions) {
                service.shutdown();
            }

            survey.setUniqueEventId(serialUid);
            try {
            executeCreateSurvey(survey);
            } catch(Exception e) {
                logger.debug("SurveyController - executeScheduled - Exception caught: ");
                e.printStackTrace();
            }
        }
    };

    service.scheduleAtFixedRate(runnable, delay, interval, TimeUnit.MILLISECONDS);
}

在没有ScheduleExecutorService的情况下运行executeCreateSurvey(survey)方法时,它可以正常运行。 然而,当它在run()方法中执行时,我将&#34;分离的实体传递给persist&#34;每次在executeCreateSurvey()方法....中运行保存(调查)方法时出错。

调用.save()方法的executeCreateSurvey()方法:

public ResponseEntity<?> executeCreateSurvey(Survey eventObject) {
    MailService mailService = new MailService(applicationProperties);

    Participant eventOwner = participantRepositoryImpl.createOrFindParticipant(eventObject.getEventOwner());
    eventObject.setEventOwner(eventOwner);

    Survey survey = surveyRepositoryImpl.createSurveyOrFindSurvey((Survey) eventObject);

    // Saves additional information if small errors (content
    // errors,.. )
    // occurs
    String warnMessage = "";
    List<Participant> participants = new ArrayList<Participant>();
    for (Participant participantDetached : eventObject.getParticipants()) {

        // Check if participant already exists
        Participant participant = participantRepositoryImpl.createOrFindParticipant(participantDetached);
        participants.add(participant);

        // Only create PartSur if not existing (Update case)
        if (partSurRepository.findAllByParticipantAndSurvey(participant, survey).isEmpty()) {
            PartSur partSur = new PartSur(participant, survey);
            partSurRepository.save(partSur);

            try {
                mailService.sendRatingInvitationEmail(partSur);
                surveyRepository.save(survey);
            } catch (Exception e) {
                // no special exception for "security" reasons
                String errorMessage = "error sending mail for participant: " + e.getMessage() + "\n";
                warnMessage += errorMessage;
                logger.warn("createSurvey() - " + errorMessage);
            }
        }
    }

    // Delete all PartSurs and Answers from removed participants
    List<PartSur> partSursForParticipantsRemoved = partSurRepository.findAllByParticipantNotIn(participants);
    logger.warn("createSurvey() - participants removed: " + partSursForParticipantsRemoved.size());
    partSurRepositoryImpl.invalidatePartSurs(partSursForParticipantsRemoved);

    return new ResponseEntity<>("Umfrage wurde angelegt. Warnungen: " + warnMessage, HttpStatus.OK);
}

原因是什么? 到目前为止,我无法在任何地方找到这个问题。

0 个答案:

没有答案