-2

もう一度試したコードを実行するとこの例外が発生しますが、継続的に問題が発生しています。今回は1人がテストを行っているため、同期タイプのエラーが発生しているため、学生はテストを行うことができませんでした

....:

Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

私のコード:

public synchronized StudentAnswer saveAnswer(User user, String assignmentid, String answerid, String answer) throws Exception {

    if (user == null || user.getStudent() == null)
        throw new GeneralSecurityException("Permission Denied");

    Auditor auditor = new Auditor(user);

    AssignmentStatus status = getAssignmentStatus(user, assignmentid);
    StudentAnswer studentAnswer = getStudentAnswer(user, assignmentid, answerid);

    Object oldStatus = auditor.clone(status, new AssignmentStatus());
    //Object oldAnswer = auditor.clone(studentAnswer, new StudentAnswer());
    Object oldAnswer = auditor.clone((studentAnswer == null) ? new StudentAnswer() : studentAnswer, new StudentAnswer());
    Date now = new Date();

    if (status != null && status.isCompleted())
        return studentAnswer;

    if (studentAnswer == null) {

        Assignment assignment   = getAssignment(user, assignmentid);
        AnswerKey answerKey     = (AnswerKey) genericDao.get(AnswerKey.class, answerid);

        if (answerKey == null)
            throw new Exception("There was an error trying to save an answer.");

        if (status == null)
            status = startAssignment(user, assignment, now);

        if (status == null)
            throw new Exception("There was an error trying to start the assignment.");

        studentAnswer = new StudentAnswer();
        studentAnswer.setStudent(user.getStudent());
        studentAnswer.setCreateTime(now);
        studentAnswer.setAssignmentStatus( status );
        studentAnswer.setAssignment(assignment);
        studentAnswer.setAnswerKey( answerKey );
        studentAnswer.setQuestionNumber( answerKey.getNumber() );
        studentAnswer.setAnswerKeyCopy( answerKey.getAnswer() );

        //Adding a new answer changes your progress percentage, so we recalculate it
        updateProgress(status, now, answer);

    }
    //changing existing answer
    else {
        Long practiceId = studentAnswer.getAnswerKey().getPractice().getObjectid();

        PracticeStatus practiceStatus = null;
        for (PracticeStatus tmpPracticeStatus : (List<PracticeStatus>) status.getPracticeStatuses()) {
            if (tmpPracticeStatus.getCustomPractice().getPractice().getObjectid().longValue() == practiceId.longValue()) {
                practiceStatus = tmpPracticeStatus;
            }
        }

        //completed practice due to time out or submit?  Don't change the
        //answer.
        if (practiceStatus != null && practiceStatus.isCompleted()) {
            throw new Exception("Time is up for this practice, so answers may no longer be changed.");
            //return studentAnswer;
        }

    }

    if (answer!=null && answer.length() > 0)
        studentAnswer.setAnswer(answer.charAt(0));
    else
        studentAnswer.setAnswer(' ');

    studentAnswer.setUpdateTime(now);


    studentAnswer = (StudentAnswer) genericDao.save(studentAnswer);

    updateGrade(status);
    updateProgress(status, now, null);
    genericDao.save(status);

    auditor.audit("answer_save", status, oldStatus);
    auditor.audit("answer_save", studentAnswer, oldAnswer);
    genericDao.saveList(auditor.getList());

    return studentAnswer;
}
4

2 に答える 2

0

おそらく、更新しようとしている行が、トランザクションがコミットされる前に、他のスレッドによってすでに更新されていることを意味します。同じレコードが複数のスレッドで同時に処理されていないか確認してください。

于 2013-02-14T09:39:46.230 に答える