Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-2386

MongoTemplate.insert throws java.lang.LinkageError exceptions

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Works as Designed
    • Affects Version/s: 2.1.11 (Lovelace SR11)
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Linux

      Description

      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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mp911de Mark Paluch
              Reporter:
              Luk Morbee Luk Morbee
              Last updater:
              Mark Paluch
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: