Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-boot mongodb solve exception 251

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.

like image 851
Gabriel García Garrido Avatar asked Aug 16 '26 02:08

Gabriel García Garrido


1 Answers

Ensure that the first request to Mongodb in the transaction is not done in parallel with any other request in that transaction. We had a similar error in one of our projects. After spending a lot of time we came to this hypothesis. For the Spring Mongo transaction, the actual transaction is started when the first request is sent to the database. It does not matter what kind of request - find, query, update, save etc. So, when the first request is sent in parallel with any other requests, but was not able to complete before them, and they think that the transaction has been started (but it hasn't), then we see this error occur. "The transaction x+1 does not exist. Last transaction is x".

To fix this error, we made changes to our flow so that when the first request is made to MongoDB, there are no parallel DB calls.

like image 83
Anagh Hegde Avatar answered Aug 18 '26 11:08

Anagh Hegde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!