0

在这里,我提供了引发异常的休眠项目代码

例外:

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

当我们调用下面提到的方法时,我想让这个线程安全

public String saveAnswer(String assignmentid, String answerid, String answer) throws Exception {

        getStudentManager().logRequestedAnswer(answerid, answer);

        User user = getUserManager().getCurrentUser();

        HttpSession session = WebContextFactory.get().getSession();
        int practiceElapsedTime = getStudentManager().touchPracticeTime(assignmentid, session);
        long practiceUpdateTime = ((Date) session.getAttribute("student_current_practice_last_update")).getTime();

        StudentAnswer savedAnswer = getStudentManager().saveAnswer(user, assignmentid, answerid, answer);

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("answerid", savedAnswer.getAnswerKey().getObjectid());
        result.put("answer", savedAnswer.getAnswer());

        //we're synching the client's time with the server on saves
        result.put("practiceElapsedTime", practiceElapsedTime);
        result.put("practiceUpdateTime", practiceUpdateTime);

        if (savedAnswer.getAssignmentStatus().isCompleted())
            result.put("progress", savedAnswer.getAssignmentStatus().getPercent());
        else
            result.put("progress", savedAnswer.getAssignmentStatus().getProgress());

        StringWriter json = new StringWriter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(json, result);
        return json.toString();

    }
4

2 回答 2

1

There are two things i learned with hibernate. First, if you save the same object multiple times rapidly you may have a StaleObjectStateException. This is due to versioning on the table. A simple solution is to disable it. You should consider locking too if your table is accessed by multiple threads: http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html

于 2013-02-14T09:53:59.273 回答
0

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

于 2013-02-14T09:40:39.303 回答