Question

I am trying to learn MongoDB and in the same time write a simple REST application using Spring framework.

I have a simple model:

@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;
}
}

Then I have a simple DAO:

@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; 
}

The I have my integration tests:

@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);
}
}

My configuration:

 <!-- MongoDB host -->
 <mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/> 

 <!-- Template for performing MongoDB operations -->
 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" 
c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/>

I am expecting that, on adding "permission_2" their would be a exception thrown from MongoDB, which would be translated by Spring,, and catched as a DataAccessException in the DAO.

Looking at the log files from MongoDb I can see that a duplicated exception is thrown but it never reaches my DAO.

So,, I guess I am doing something wrong,,, but at the moment,, I am blind to my own misstakes.

//lg

Was it helpful?

Solution

Make sure you configure the WriteConcern of the MongoTemplate to something non-default (e.g. WriteConcern.SAFE). By default MongoDB is in fire-and-forget mode and does not throw exceptions on index violations or server errors in general.

OTHER TIPS

Still struggling with this. Finnally I succeded to get the exeption translation working. MongoDb throws a exception which is translated to Spring Data exception.

Now I am stuck with another problem.

My DAO shown above has also the following code:

     @ExceptionHandler(DataAccessException.class)
     public void handleDataAccessException(DataAccessException ex) {
            // For debug only
                            DataAccessException test = ex;
            test.printStackTrace();

      }

I was expecting this code to catch the exception thrown,, but this is not the case.

Why not?

//lasse

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top