Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoTemplate.insert throws java.lang.LinkageError exceptions [DATAMONGO-2386] #3244

Closed
spring-projects-issues opened this issue Oct 3, 2019 · 6 comments

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Oct 3, 2019

Luk Morbee opened DATAMONGO-2386 and commented

After many inserts into MongoDB (using MongoTemplate.insert) we get java.lang.LinkageError exceptions (see below).

We think the bug is in the ClassGeneratingPropertyAccessorFactory because no exceptions are thrown if you would use the BeanWrapperPropertyAccessorFactory instead.

This can be reproduced with the following code (takes about 100K iterations).

 

import java.util.stream.IntStream;import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;

public class SpringDataBug {
    public static void main(String[] args) {
        IntStream.range(0, 1_000_000).forEach(i -> {
            MyBean bean = new MyBean(i);
            TypeInformation<MyBean> typeInformation = ClassTypeInformation.from(MyBean.class);
            BasicMongoPersistentEntity persistentEntity = new BasicMongoPersistentEntity<>(typeInformation);
            try {
                Property idProperty = Property.of(ClassTypeInformation.from(MyBean.class), MyBean.class.getDeclaredField("id"));
                persistentEntity.addPersistentProperty(new CachingMongoPersistentProperty(idProperty, persistentEntity, SimpleTypeHolder.DEFAULT, null));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            persistentEntity.setPersistentPropertyAccessorFactory(new ClassGeneratingPropertyAccessorFactory());
            IdentifierAccessor identifierAccessor = persistentEntity.getIdentifierAccessor(bean);
            System.out.println(identifierAccessor.getIdentifier());
        });
    }

    private static class MyBean {

        int id;

        public MyBean(int id) {
            this.id = id;
        }        public int getId() {
            return id;
        }
    }
}

 

It throws the following exception:

Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app') at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:200) at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.potentiallyCreateAndRegisterPersistentPropertyAccessorClass(ClassGeneratingPropertyAccessorFactory.java:184) at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.getPropertyAccessor(ClassGeneratingPropertyAccessorFactory.java:92) at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:455) at org.springframework.data.mapping.model.IdPropertyIdentifierAccessor.<init>(IdPropertyIdentifierAccessor.java:54) at org.springframework.data.mapping.model.BasicPersistentEntity.getIdentifierAccessor(BasicPersistentEntity.java:471) at SpringDataBug.lambda$main$0(SpringDataBug.java:26) at java.base/java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:104) at java.base/java.util.stream.IntPipeline$Head.forEach(IntPipeline.java:593) at SpringDataBug.main(SpringDataBug.java:15)Caused by: java.lang.IllegalStateException: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app') at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$PropertyAccessorClassGenerator.generateCustomAccessorClass(ClassGeneratingPropertyAccessorFactory.java:326)Caused by: java.lang.IllegalStateException: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app')
 at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:198) ... 9 moreCaused by: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app') at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:530)Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app')
 at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$PropertyAccessorClassGenerator.generateCustomAccessorClass(ClassGeneratingPropertyAccessorFactory.java:324) ... 10 moreCaused by: java.lang.LinkageError: loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app') at java.base/java.lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)Caused by: java.lang.LinkageError: loader 'app' attempted duplicate class definition for SpringDataBug$MyBean_Accessor_4j0ajz. (SpringDataBug$MyBean_Accessor_4j0ajz is in unnamed module of loader 'app')
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:527) ... 11 more

Affects: 2.1.11 (Lovelace SR11)

Reference URL: spring-projects/spring-framework#23730

Issue Links:

  • DATACMNS-1602 Reuse generated PropertyAccessor's and Entityinstantiator classes across EntityInstantiators and PersistentPropertyAccessorFactorys
@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 3, 2019

Mark Paluch commented

The code above creates new instances of ClassGeneratingPropertyAccessorFactory. You must reuse ClassGeneratingPropertyAccessorFactory as it keeps a cache of generated classes. In consequence, the code attempts to redefine the same class multiple times.

Care to elaborate on your use case as the direct usage of Spring Data Commons SPI isn't recommended from within applications

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 3, 2019

Luk Morbee commented

Hi Mark,

With only one instance of the ClassGeneratingPropertyAccessorFactory, the code above wasn't able to reproduce the issue (even in parallel).

But we do encounter this issue just by using the MongoTemplate.insert() method.
We only made direct usage of Spring Data Commons SPI to try to reproduce the issue.

We have a long running job that inserts millions of documents into a MongoDB collection and after 3 hours we run into this java.lang.LinkageError.

Just guessing here but could Spring Data be doing some householding tasks that would trigger the creation of multiple ClassGeneratingPropertyAccessorFactory instances?

Thanks

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 3, 2019

Mark Paluch commented

If you keep a single MongoTemplate instance, specifically a single MongoMappingContext, there should be no need to re-define property-accessor classes.
org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.PropertyAccessorClassGenerator is the component that generates and defines classes during runtime and its outer class, ClassGeneratingPropertyAccessorFactory holds strong references to types and classes. This case sounds as if GC pressure causes the disposal of classes and on subsequent accesses to previously seen classes property accessors get re-generated.

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 7, 2019

Luk Morbee commented

Our app did create multiple MongoTemplate instances pointing to the same MongoDB database. Using a single MongoTemplate instance does indeed solve the problem.

This still seems to be a bug though.

Anyway, thanks for your prompt replies

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 17, 2019

Mark Paluch commented

If you reuse MongoConverter, or at least EntityInstantiators that is associated with the converter when creating a new MongoTemplate, then everything works fine

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 17, 2019

Mark Paluch commented

Closing this one as everything works as designed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants