1

Following this tutorial, working with complete code, how do I enable exception translation for mongo?

When my mongo db is down, i'm getting 500 error from com.mongodb.MongoServerSelectionException. Shouldn't this be translated into DataAccessResourceFailureException with MongoExceptionTranslator? Am i supposed to register this bean somehow? I've tried with:

@Bean
public MongoExceptionTranslator mongoExceptionTranslator() {
    return new MongoExceptionTranslator();
}

but still no change

EDIT:

I've created a demo with the suggestions from Stackee007 but still can't get this to work

Community
  • 1
  • 1
airborn
  • 2,547
  • 1
  • 15
  • 12

1 Answers1

0

MongoExceptionTranslator is already registered if your configuration registers MongoFactoryBean or SimpleMongoDbFactory. You could configure mongo something like below which registers SimpleMongoDbFactory.

@Configuration
@EnableMongoRepositories
public class ApplicationConfig extends AbstractMongoConfiguration {

    @Override
    protected String getDatabaseName() {
        return "yyy";
    }

    @Override
    protected UserCredentials getUserCredentials() {
        return new UserCredentials("abc", "***");
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        List<ServerAddress> seeds = new ArrayList<ServerAddress>();
        seeds.add(new ServerAddress("xxxx"));
        seeds.add(new ServerAddress("xxx"));
        seeds.add(new ServerAddress("xx"));

        MongoClient mongo = new MongoClient(seeds);
        mongo.setReadPreference(ReadPreference.secondaryPreferred());
        mongo.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        return mongo;
    }

    @Bean
    public GridFsTemplate gridFsTemplate() throws Exception {
        return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
    }

}
Stackee007
  • 3,196
  • 1
  • 26
  • 39
  • This didn't help, i'm still seeing MongoServerSelectionException `{"timestamp":1423912384275,"status":500,"error":"Internal Server Error","exception":"com.mongodb.MongoServerSelectionException","message":"Unable to connect to any servers","path":"/people"}` – airborn Feb 14 '15 at 11:15
  • This is how this looks now https://github.com/airborn/gs-accessing-mongodb-data-rest/tree/abstractMongoConfiguration/complete/src/main/java/hello – airborn Feb 14 '15 at 11:40