저는 (Eclipselink를 사용하여) 그렇게 기대하지 않은 JPA 동작에 당황합니다.커밋되지 않은 JPA find() 메소드를 읽지 않은 이유는 무엇입니까?
나는 Stateless 세션 EJB (3.2) 인 Wildfly 10 (JDK-8)에서 실행됩니다. 내 메서드 호출은 - 기본적으로 - 트랜잭션에 캡슐화됩니다. 엔터티 빈을 읽고 업데이트 할 때 비즈니스 방법으로 업데이트, 특히 엔터티의 버전 번호를 인식하지 못했습니다. 그래서
org.eclipse.persistence.exceptions.OptimisticLockException
내 코드에서 내 전화의 결과는이 같은 단순화 된 모양 : 나는
주석 방법에 (엔티티 빈의 데이터를 변경하고 플러시) 코드를 넣으면public ItemCollection process(MyData workitem) {
....
// load document from jpa
persistedDocument = manager.find(Document.class, id);
logger.info("@version=" + persistedDocument.getVersion());
// prints e.g. 3
// change some data
....
manager.flush();
logger.info("@version=" + persistedDocument.getVersion());
// prints e.g. 4
....
// load document from jpa once again
persistedDocument = manager.find(Document.class, id);
logger.info("@version=" + persistedDocument.getVersion());
// prints e.g. 3 (!!)
// change some data
....
manager.flush();
// Throws OptimisticLockException !!
// [email protected]] cannot be updated because it has changed or been deleted since it was last read
...
}
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
모든 것이 예상대로 작동합니다.
하지만 내 코드에서 find() 메서드의 두 번째 호출이 새 버전 번호를 읽지 않는 이유는 무엇입니까? 나는 flush()와 find() 호출 후에 버전 4를 기대할 것이다.
두 번째 동일한 트랜잭션에서 호출 하시겠습니까? –
예 이것들은 모두 하나의 동일한 상태 저장 세션 EJB 메소드 – Ralph
에서 실행됩니다. 다른 프로젝트에서 비슷한 코드를 사용했다고 생각합니다.하지만 OneToMany 관계가 많습니다. 지금과 다른 동작이있는 것 같습니다. – Ralph