MongoDB를 배우는 동시에 스프링 프레임 워크를 사용하여 간단한 REST 애플리케이션을 작성하려고합니다.스프링 데이터 MongoTemplate이 DataAccessException을 던지지 않음
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:mvc-dispatcher-servlet.xml", classpath:IntegrationContext.xml"},loader = TestXmlContextLoader.class)
@ActiveProfiles("mongoIntegrationTest")
public class RepositoryIntegrationTest extends AccountTestBase{
@Autowired DAO repository;
@Autowired WebApplicationContext wac;
@Test
public void AddPermission() {
Permission permission_1 = new Permission("test");
Permission permission_2 = new Permission("test");
repository.addObject(permission_1);
repository.addObject(permission_2);
}
}
내 구성 :
@Repository
@Transactional
@Profile({"production","repositoryTest","mongoIntegrationTest"})
public class DaoImpl implements DAO {
@Autowired
protected MongoTemplate mongoTemplate;
public <T> T addObject(T object) {
mongoTemplate.insert(object);
return object;
}
(가) 내 통합 테스트를 : 나는 간단한 DAO를 다음
@Document
public class Permission extends documentBase{
@Indexed(unique = true)
private String name;
public Permission(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
:
나는 간단한 모델을 가지고"permission_2"를 추가하면 MongoDB에서 던져진 예외가 Spring에서 번역되고 DAO에서 DataAccessException으로 catch됩니다.
MongoDb에서 로그 파일을 보면 복제 된 예외가 발생하지만 내 DAO에는 도달 할 수 없음을 알 수 있습니다.
그래서, 내가 뭔가 잘못하고있는 것 같아요.하지만, 지금은, 저 자신의 잘못을 눈이 멀었습니다.
// LG
mongoTemplate.setWriteConcern (WriteConcern.SAFE); 그래도 예외는 없습니다. 몽고 셸에서 이것을 볼 수 있습니다 : Thu Oct 04 18:32:48 [conn9] insert ActiveDb.permission 예외 : E11000 중복 키 오류 인덱스 : ActiveDb.permission. $ name dup key : {: "test"} 코드 : 11000 0ms –
+1 "Fire and Forget"에 대한 +1 : –