본문 바로가기

컨트리뷰션/DataJPA

4. batch 메서드 컨트리뷰션 - org.hibernate.AssertionFailure: possible non-threadsafe access to session

 

deletaAllInBatch(Iterable<T> entities) 메서드를 수정하는 과정에서 다음과 같은 예외가 발생하면 테스트 코드에 실패했습니다.

 

	@Override
	@Transactional
	public void deleteAllInBatch(Iterable<T> entities) {

		Assert.notNull(entities, "Entities must not be null");

		if (!entities.iterator().hasNext()) {
			return;
		}

		//컨트리뷰션 시작
		for (T entity : entities) {
			if (entityManager.contains(entity)) {
				entityManager.remove(entity);
				entityManager.detach(entity);
			}
		}
        //컨트리뷰션 종료

		applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities, entityManager)
				.executeUpdate();
	}

 

possible non-threadsafe access to session

 

해당 예외에 대해 찾아보니 Hibernate 내부 버그일 수 있다고 합니다...

그러나 저는 엔티티 매니저의 detach 메소드를 호출해 DB에 단건 삭제 쿼리가 나가는 것을 방지하고 싶습니다.

이에 대한 해결책으로 em.clear()를 하여 엔티티 매니저가 가지고 있는 모든 엔티티를 비워버리는 방식을 선택하였습니다.

 

이러한 해결책에 대해 걱정되는 상황이 몇가지 있었으나 아래와 같이 생각해 em.clear()를 선택했습니다.

  1. 배치성 작업을 띄는 메소드이므로 기타 비즈니스 로직이 필요한 상황이 발생할 확률은 낮다.
  2. 삭제쿼리 이후 영속성 컨텍스트에 존재하는 엔티티에 의존하는 로직이 추가될 가능성은 낮다.