Spring boot:处理mongodb timeoutException

时间:2017-05-16 08:09:45

标签: mongodb spring-boot spring-data-mongodb

当Mongodb关闭时,Spring启动应用程序将关闭。我希望处理connectiontimeout的异常并在不停止应用程序的情况下记录错误。 当从数据库中查找项目失败,因为无法建立连接时,应用程序应该执行另一种处理,例如调用Web服务来查找数据。 你对此有什么想法吗?

配置

  

spring.data.mongodb.uri = mongodb的:// $ {MONGODB_DB_HOST}:$ {MONGODB_DB_PORT} / $ {MONGODB_DB_DATABASE} connectTimeoutMS = $ {mongodb.connection.timeout}

2 个答案:

答案 0 :(得分:0)

我使用下面的代码在spring boot中配置mongodb连接

您可以根据需要指定套接字超时和连接超时。

@Configuration
public class DatabaseConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseConfiguration.class);


    @Value("${spring.data.mongodb.uri}")
    private String mongoUri;

    @Value("${spring.data.mongodb.database}")
    private String mongoDbName;

    @Value("${spring.data.mongodb.host}")
    private String host;

    @Value("${spring.data.mongodb.port}")
    private int port;

    @Value("${spring.data.mongodb.username}")
    private String username;

    @Value("${spring.data.mongodb.password}")
    private String password;

    @Bean
    public MongoTemplate mongoTemplate() {

        LOGGER.debug(" instantiating MongoDbFactory ");

        SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient(), mongoDbName);

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);

        return mongoTemplate;

    }

    @Bean
    public MongoClient mongoClient() {

        List<ServerAddress> servers = new ArrayList<ServerAddress>();
        servers.add(new ServerAddress(host, port));

        MongoClientOptions mongoClientOptions = MongoClientOptions.builder()
                .connectionsPerHost(10)
                .socketTimeout(2000)
                .connectTimeout(2000)
                .build();

        if (Utils.isNotEmpty(username) && Utils.isNotEmpty(password)) {
            List<MongoCredential> creds = new ArrayList<MongoCredential>();
            creds.add(MongoCredential.createCredential(username, mongoDbName, password.toCharArray()));
            return new MongoClient(servers,creds, mongoClientOptions);
        } else
            return new MongoClient(servers, mongoClientOptions);
    }

    @Bean
    public MongoClientURI mongoClientURI() {
        LOGGER.debug(" creating connection with mongodb with uri [{}] ", mongoUri);
        return new MongoClientURI(mongoUri);
    }

}

在application.yml文件中定义以下属性

mongodb特定属性

spring:
  data:
    mongodb:
      database: dbname
      host: localhost
      port: 27017
      username: dbusername
      password: dbpassword

答案 1 :(得分:-2)

您可以使用以下示例示例配置mongodb超时。我希望将springboot转换为bean annonation很容易。否则,您可以导入资源bean(@ImportResource)

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<label>
  <input type="checkbox" data-bind="checked: showLargeTable, disable: working">Show slowly rendered table
</label>

<table data-bind="css: { 'is-loading': working }">
  <tbody data-bind="foreach: data">
    <tr>
      <td data-bind="text: name"></td>
      <td data-bind="text: position"></td>
      <td data-bind="text: location"></td>
    </tr>
  </tbody>
</table>

<em>Example based on the <a href="http://knockoutjs.com/documentation/deferred-updates.html">knockout docs</a></em>