MongoDB - Spring - Saving an object causes StackOverflowError
Asked Answered
B

2

5

Our architecture combines Spring with MongoDB. We usually do not have any issues with saving/editing and reading custom objects. Lately we have ran a few modifications in which we started getting the following errors

       java.lang.StackOverflowError
at java.util.Collections.emptyList(Collections.java:2959)
at org.springframework.data.util.TypeDiscoverer.getTypeArguments(TypeDiscoverer.java:442)
at org.springframework.data.util.ClassTypeInformation.getTypeArguments(ClassTypeInformation.java:40)
at org.springframework.data.util.TypeDiscoverer.getActualType(TypeDiscoverer.java:288)
at org.springframework.data.util.ClassTypeInformation.getActualType(ClassTypeInformation.java:40)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.addCustomTypeKeyIfNecessary(MappingMongoConverter.java:650)

Or

        java.lang.StackOverflowError
at sun.misc.Unsafe.getObject(Native Method)
at sun.misc.Unsafe.getObject(Unsafe.java:231)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:19)
at java.lang.reflect.Field.get(Field.java:358)
at org.springframework.util.ReflectionUtils.getField(ReflectionUtils.java:118)
at org.springframework.data.mapping.model.BeanWrapper.getProperty(BeanWrapper.java:133)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:382)

Before I'm adding all the major classes that combines our architecture I wonder whether any of you have previously encountered these kind of exceptions?

Baran answered 12/2, 2014 at 17:11 Comment(0)
B
9

Apparently I can not add logger to my data objects,

when I removed the logger it started working again

protected final Log logger = LogFactory.getLog(getClass());

Be careful when using loggers in your data object in MongoDB

It seems that using @Transient should signal MongoDB driver not to persist the logger variable

http://docs.spring.io/spring-data/data-document/docs/current/reference/html/#mapping-usage-annotations

Baran answered 12/2, 2014 at 19:3 Comment(6)
woah, glad that you figured that out. does it help by declaring logger as private?Hipped
mark it as either static (preferred) or transient (because, why not?) and you should be fine. You really don't want a logger per instance.Honan
I have to check, we use MongoDB driver which is still not very mature, I was shocked when I realized it tried to persist a final logger variable. I read somewhere that if I use transient it will be okay, I will have to verify that.Baran
@Transient will do the trick docs.spring.io/spring-data/data-document/docs/current/reference/…Baran
@Honan my field is "transient" and I am hitting the same issue. It is sad that we need to bound ourselves to a custom annotation Transient insteadOldfangled
thank you for this... i had almost no hair left! ^^Overmodest
T
0

I have faced this issue while fetching document from mongo database using findById method so as workaround you can use another alternatives supported mongoTemplate find methods .

Thanks.

Taliesin answered 10/6, 2022 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.