javaspringmongodbspring-bootmongorepository

StackOverflowError while using MongoRepository save/insert


I am trying to use a mongo repository to save/insert a wrapper Key-Value Class into my mongoDB.

The model which I am trying to save is:

@Data
@AllArgsConstructor
@Document(collection = "futureTask")
public class FutureTask {

    @Id
    private Configuration configuration;
    private ScheduledFuture<?> future;
}

The repository was implemented as follows:

public interface FutureTaskRepository extends MongoRepository<FutureTask, Configuration> {
    FutureTask findByConfiguration(Configuration configuration);

    boolean existsByConfiguration(Configuration configuration);
}

the call of FutureTaskRepository is made as follows:

ScheduledFuture<?> future = this.schedule(new RunnableTask(configuration), new CronTrigger(configuration.getCron()));
FutureTask futureTask = new FutureTask(configuration, future);

futureTaskRepository.insert(futureTask);

I get following error:

java.lang.StackOverflowError: null
    at java.base/java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryReleaseShared(ReentrantReadWriteLock.java:427) ~[na:na]
    at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.releaseShared(AbstractQueuedSynchronizer.java:1382) ~[na:na]
    at java.base/java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.unlock(ReentrantReadWriteLock.java:897) ~[na:na]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:244) ~[spring-data-commons-2.3.5.RELEASE.jar:2.3.5.RELEASE]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:206) ~[spring-data-commons-2.3.5.RELEASE.jar:2.3.5.RELEASE]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:90) ~[spring-data-commons-2.3.5.RELEASE.jar:2.3.5.RELEASE]
    at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:73) ~[spring-data-commons-2.3.5.RELEASE.jar:2.3.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:694) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:700) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:700) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:700) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:700) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:700) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:700) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:620) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:596) ~[spring-data-mongodb-3.0.5.RELEASE.jar:3.0.5.RELEASE]

Solution

  • monoDB seems to have an issues with persisting objects like that. made an workaround with creating an custom repository with a synchronizedCollection storage. not using mongoDB or any other DB now.

    @Repository
    public class FutureTaskRepository {
    
        // collection of futureTasks
        private Collection<FutureTask> futureTasks = Collections.synchronizedCollection(new ArrayList<FutureTask>());
    
        public Collection<FutureTask> findAll() {
            return this.futureTasks;
        }
    
        public FutureTask find(FutureTask futureTask) {
            Optional<FutureTask> optional = this.futureTasks.stream().filter(task -> task.equals(futureTask)).findFirst();
            return optional.orElse(null);
        }
    
        public FutureTask findByConfiguration(Configuration configuration) {
            Optional<FutureTask> optional = this.futureTasks.stream().filter(task -> task.getConfiguration()
                    .equals(configuration)).findFirst();
    
            return optional.orElse(null);
        }
    
        public void insert(FutureTask futureTask) {
            this.futureTasks.add(futureTask);
        }
    
        public void deleteAll() {
            this.futureTasks.clear();
        }
    
        public void delete(FutureTask futureTask) {
            FutureTask task = this.find(futureTask);
            this.futureTasks.remove(task);
        }
    
        public boolean exists(FutureTask futureTask) {
            return this.futureTasks.contains(futureTask);
        }
    
        public boolean existsByConfiguration(Configuration configuration) {
            FutureTask futureTask = this.findByConfiguration(configuration);
            return this.exists(futureTask);
        }
    }