title: Springboot - MongoDb: prematurely reach end of stream
body: <p>I got the following issue when trying to send a big request to server.</p>
  
  <pre><code>com.mongodb.MongoSocketReadException: Prematurely reached end of stream
          at com.mongodb.connection.SocketStream.read(SocketStream.java:88) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:96) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:440) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:143) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.UpdateOperation.executeCommandProtocol(UpdateOperation.java:89) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:139) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:133) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:422) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:413) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60) ~[mongodb-driver-core-3.4.2.jar!/:na]
          at com.mongodb.Mongo.execute(Mongo.java:845) ~[mongodb-driver-3.4.2.jar!/:na]
          at com.mongodb.Mongo$2.execute(Mongo.java:828) ~[mongodb-driver-3.4.2.jar!/:na]
          at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:342) ~[mongodb-driver-3.4.2.jar!/:na]
          at com.mongodb.DBCollection.replaceOrInsert(DBCollection.java:420) ~[mongodb-driver-3.4.2.jar!/:na]
          at com.mongodb.DBCollection.save(DBCollection.java:409) ~[mongodb-driver-3.4.2.jar!/:na]
          at com.mongodb.DBCollection.save(DBCollection.java:382) ~[mongodb-driver-3.4.2.jar!/:na]
          ...
  </code></pre>
  
  <p>According to my investigate, this error happens because the connection to MongoDb is not kept alive. So I try to add some config to extend the timeout:</p>
  
  <pre><code>@SpringBootApplication
  @EnableResourceServer
  @EnableDiscoveryClient
  @EnableFeignClients
  @EnableOAuth2Client
  @EnableConfigurationProperties
  @Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  @EnableWebSecurity
  
  public class CodelabArticleServiceApplication extends ResourceServerConfigurerAdapter {
  
  public static void main(String[] args) {
      SpringApplication.run(CodelabArticleServiceApplication.class, args);
  }
  
  @Override
  public void configure(HttpSecurity http) throws Exception {
      // @formatter:off
      http.authorizeRequests().antMatchers("/*").permitAll().anyRequest().authenticated();
      // @formatter:on
  }
  
  @Bean
  @ConfigurationProperties(prefix = "security.oauth2.client")
  public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
      return new ClientCredentialsResourceDetails();
  }
  
  @Bean
  public MongoClientOptions mongoOptions() {
      int timeout = 3600000;
      return MongoClientOptions.builder().socketKeepAlive(true).maxConnectionIdleTime(timeout)
              .maxConnectionLifeTime(timeout).maxWaitTime(timeout).socketTimeout(timeout).build();
  }
  
  @Bean
  public RequestInterceptor oauth2FeignRequestInterceptor() {
      return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails());
  }
  
  @Bean
  public OAuth2RestTemplate clientCredentialsRestTemplate() {
      return new OAuth2RestTemplate(clientCredentialsResourceDetails());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>For some reasons, It's still <strong>not</strong> working. I wonder if my config was right or there are some additional task to make this work?</p>