Spring-boot mongodb solve exception 251

Viewed 480

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.

0 Answers
Related