I am trying to understand how I can shutdown the connection to mongo while my java application runs for tesing purposes. I am trying to see how my application would behave in case mongo is unavailable. Is there any way this accomplished at the application level without touching the mongo database?. This is the class I use to create the mongo connection
@Configuration
@EnableMongoAuditing
public class MongoConfiguration {
@Autowired
private MongoProperties mongoProperties;
@Bean
public MongoClient mongo(){
ConnectionString connectionString = new ConnectionString(mongoProperties.getUri());
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
return MongoClients.create(mongoClientSettings);
}
// to use @Transactional -- supporting spring data transaction support in MongoDB
@Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory){
return new MongoTransactionManager(dbFactory);
}
@Primary
@Bean(name = "mongoTemplate")
public MongoTemplate mongoTemplate(){
return new MongoTemplate(mongo(), mongoProperties.getDatabase());
}
}
You can use fail points. See https://github.com/mongodb/specifications/tree/master/source/transactions/tests#failcommand for examples and you need to start the server with --setParameter enableTestCommands=1
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.