컨트롤러 로직 :JPA와 StaleObjectStateException를 해결하고 최대 절전 모드하는 방법
def updateObject() {
Object o = Object.get(params.id as Long)
o.otherObjects.clear()
objectDataService.saveObject(o.id)
OtherObject newObject = new OtherObject;
o.addToOtherObjects(newObject)
objectDataService.saveObject(o.id)
}
ServiceLogic
def saveObject(long profileId) {
o.save(flush:true)
}
이 그냥 작동 할 경우 90 %에
을 무슨.
문제
ERROR errors.GrailsExceptionResolver - StaleObjectStateException occurred when processing request: [GET] /controller/updateObject - parameters:
stuff[]: data
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.path.Object#1].
Stacktrace follows:
Message: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.path.Object#1]
내가 관련된 질문을 읽고 당신은 아는 merge
전화를 발견했다. 그것은 약 50 %의 경우를 해결했지만 전부는 아닙니다.
이 로직을 서비스 메소드로 옮긴다면 모든 트랜잭션이 동일하게 처리되었으므로 여기서 더 나아지지 않을까요? –
서비스를 오버로드하지 않으려합니다. 이해가 되겠습니까? –
비즈니스 로직을 서비스로 옮기고 컨트롤러를 가능한 한 얇게 만드는 것이 더 좋습니다 (코드의 줄이 작아짐에 따라 씬이 줄어 듭니다). 이렇게하면 필요할 때마다 코드를 재사용하고 테스트에도 많은 시간을 절약 할 수 있습니다. –