Spring-boot mongodb solve exception 251
Asked Answered
C

1

7

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.

Coppock answered 1/7, 2021 at 11:21 Comment(4)
Are you sending multiple operations to Mongo in parallel in the same transaction?Indochina
Could you give your mongo version ? And maybe show us logs from the mongo server ? They can be quite useful if they're verbose.Harned
This answer may provide some insight although it refers to a node.js implementation: https://mcmap.net/q/603718/-how-to-prevent-quot-given-transaction-number-1-does-not-match-any-in-progress-transactions-quot-with-mongoose-transactions And it would help to know how you're executing queries and the context of those with regard to concurrency.Bowyer
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 accessVetavetch
E
1

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.

Enclasp answered 26/4, 2023 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.