Error creating snapshots in AXON 4.1.1: An event for aggregate [] at sequence [] could not be persisted

We are using axon version 4.1.1 and axon-mongo version 4.4. We added the configuration for snapshots in our config class. The code of our config class is added below:

@Configuration
public class EventStoreConfig {

@Autowired
private  transient MongoClient mongoClient;

@Autowired
private ConnectionFactory connectionFactory;

@Bean
public SimpleCommandBus commandBus(TransactionManager txManager, AxonConfiguration axonConfiguration) {
    SimpleCommandBus commandBus =
            SimpleCommandBus.builder()
                    .transactionManager(txManager)
                    .messageMonitor(axonConfiguration.messageMonitor(CommandBus.class, "commandBus"))
                    .build();
    commandBus.registerHandlerInterceptor(
            new CorrelationDataInterceptor<>(axonConfiguration.correlationDataProviders())
    );
    return commandBus;
}

@Bean
public SimpleQueryBus queryBus(AxonConfiguration axonConfiguration, TransactionManager transactionManager) {
    return SimpleQueryBus.builder()
            .messageMonitor(axonConfiguration.messageMonitor(QueryBus.class, "queryBus"))
            .transactionManager(transactionManager)
            .errorHandler(axonConfiguration.getComponent(
                    QueryInvocationErrorHandler.class,
                    () -> LoggingQueryInvocationErrorHandler.builder().build()
            ))
            .queryUpdateEmitter(axonConfiguration.getComponent(QueryUpdateEmitter.class))
            .build();
}

@Bean
public QueryGateway queryGateway(QueryBus queryBus) {
    return DefaultQueryGateway.builder().queryBus(queryBus).build();
}

@Bean
public CommandGateway commandGateway(CommandBus commandBus) {
    return DefaultCommandGateway.builder().commandBus(commandBus).build();
}

@Bean
public EmbeddedEventStore eventStore(EventStorageEngine storageEngine, AxonConfiguration configuration) {
    return EmbeddedEventStore.builder()
            .storageEngine(storageEngine)
            .messageMonitor(configuration.messageMonitor(EventStore.class, "eventStore"))
            .build();
}


@Bean
public EventStorageEngine storageEngine() {
    return MongoEventStorageEngine.builder().eventSerializer(JacksonSerializer.builder().build()).mongoTemplate(mongoTemplate()).build();
}

@Bean
public DefaultMongoTemplate mongoTemplate() {
    return DefaultMongoTemplate.builder().mongoDatabase(mongoClient).build();
}

@Bean
public SagaStore<Object> mySagaStore() {
    return MongoSagaStore.builder().serializer(JacksonSerializer.builder().build()).mongoTemplate(mongoTemplate()).build();
}

@Bean
public TokenStore tokenStore(Serializer serializer) {
    return MongoTokenStore.builder()
            .mongoTemplate(mongoTemplate())
            .serializer(serializer)
            .build();
}

@Bean
public SnapshotTriggerDefinition mySnapshotTriggerDefinition(Snapshotter snapshotter) {
    System.out.println("mySnapshotTriggerDefinition called");
    return new EventCountSnapshotTriggerDefinition(snapshotter, 2);
}

@Bean
public SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean() {
    return new SpringAggregateSnapshotterFactoryBean();
}

}

And I have used the snapshot trigger definition with my aggregate as below:

@Aggregate(snapshotTriggerDefinition = “mySnapshotTriggerDefinition”)
public class MyAggregate implements Serializable {
}

But while my application is trying to create a snapshot it is throwing error as shown below:

o.a.eventsourcing.AbstractSnapshotter : An attempt to create and store a snapshot resulted in an exception. Exception summary: An event for aggregate [12345] at sequence [1] could not be persisted.

Can anyone suggest if I’m missing anything?

Hi @neha , that’s pretty interesting.

Can you change from 2 to maybe 5 and see if the error still occurs?
I am curious about that.

Hi @lfgcampos , Thanks for your reply.
I tried changing the threshold from 2 to 5. But the error is still there.
Then, I tried updating axon version to 4.5, and the error changed to:

An attempt to create and store a snapshot resulted in an exception. Exception summary: An event for aggregate [12345] at sequence [5] was already inserted.

But there is not any snapshot in snapshotevents collection.

Thanks for the reply.

Have you checked our mongo-axon-example?
It is using snapshots as well and everything works as expected.

I see some differences in the configuration that can help you.

Let me know if that helps!