Announcement Announcement Module
Collapse
No announcement yet.
Spring Data MongoTemplate not throwing DataAccessException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data MongoTemplate not throwing DataAccessException

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

    I have a simple model:

    Code:
    @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:

    Code:
    @Repository
    @Transactional 
    @Profile({"production","repositoryTest","mongoIntegrationTest"})
    public class DaoImpl   implements DAO  {
    
    @Autowired
    protected MongoTemplate mongoTemplate;
    
    public <T> T addObject(T object) {      
        try {
          mongoTemplate.insert(object);       
        } catch ( DataAccessException e) {
            // TODO: handle exception
        }
        finally { return object;}
    }
    The I have my integration tests:

    Code:
    @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:
    Code:
    <!-- 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.

    If no one can see any obvious mistakes,,, then please,, how do I debug this?

    //lg

  • #2
    And what did you expect to see? There is a catch and swallow in your dao basically rendering any exception thrown useless.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      And what did you expect to see? There is a catch and swallow in your dao basically rendering any exception thrown useless.
      Yes,, I know that. But I expected to catch the exception (DataAccessException ) but it is never thrown. In production code I will remove the catch,,,
      During my work to isolate my problem I added a catch,, and using the eclipse debugger I've noticed that no exception is thrown.

      //lg

      Comment

      Working...
      X