6

I am developing an spring boot application connected to a mongo db database.

I dont know why but sometimes is giving me the following error:

org.springframework.data.mongodb.MongoTransactionException: Query failed with error code 251 and error message 'Given transaction number 1 does not match any in-progress transactions. The active transaction number is -1' on server <>; nested exception is com.mongodb.MongoQueryException: Query failed with error code 251 and error message 'Given transaction number 1 does not match any in-progress transactions. The active transaction number is -1' on server <>.

I tried to read a bit more a bout this issue but I can not find to much info. I have seen that sometimes this is due timeout problems with transaction manager, but i just get the error when the request is received. As you can see in the following log

I send the request at: 2021-06-29 13:41:14,054 and I received the error at 2021-06-29 13:41:14,061, just 10 miliseconds after.

I have the following configuration:

@Configuration
public class SpringMongoconfig {

  @Autowired private MongoDatabaseFactory mongoDbFactory;

  @Autowired private MongoMappingContext mongoMappingContext;

  public @Bean MongoTemplate mongoTemplate() {

    // remove _class
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    return new MongoTemplate(mongoDbFactory, converter);
  }

  @Bean
  MongoTransactionManager txManager(MongoDatabaseFactory dbFactory) {
    return new MongoTransactionManager(dbFactory);
  }
}

It only happens sometimes because if then I launch the same request it works nice

And my ddbb is in replica mode.

Can someone help me?

Thanks in advance.

  • 1
    Are you sending multiple operations to Mongo in parallel in the same transaction? – Emerson Farrugia Dec 05 '21 at 15:29
  • Could you give your mongo version ? And maybe show us logs from the mongo server ? They can be quite useful if they're verbose. – Youri Dec 06 '21 at 16:40
  • This answer may provide some insight although it refers to a node.js implementation: https://stackoverflow.com/a/60656959/1907186 And it would help to know how you're executing queries and the context of those with regard to concurrency. – Lucas Ross Dec 08 '21 at 06:22
  • It is just with read querys. I am not sending multiple operations in parallel. Mongo version is the last spring boot data mongodb version. We have update it today to use mongo 5 with the new driver. I cant see mongo server logs because I do not have access – Gabriel García Garrido Dec 09 '21 at 10:26

0 Answers0