行被另一个事务更新或删除

时间:2013-02-14 09:36:40

标签: spring hibernate

运行代码时出现此异常 我再次尝试但我不断得到它 问题是学生无法进行测试,因为一个人这次给它 同步类型的错误即将到来

...

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

我的代码:

公共同步StudentAnswer saveAnswer(用户用户,String assignmentid,String answerid,String answer)抛出异常{

    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;
}

2 个答案:

答案 0 :(得分:0)

这可能意味着在您的事务提交之前,您尝试更新的行已经被其他一些线程更新过。检查多个线程是否同时处理相同的记录。

答案 1 :(得分:0)

我认为你的问题是为什么你得到这个例外以及如何解决它。如果您的系统对并发更新的容忍度为零,那么在查询中使用悲观锁定,但您必须确保在短事务中获得锁定,否则会出现性能问题。 或者更好的解决方案是使用乐观锁(使用版本字段)并检查版本是否等于最新版本,如果不可能向用户发送消息说数据已过期。