mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.78k stars 12.86k forks source link

graalvm build failure #2414

Open TruthBean opened 2 years ago

TruthBean commented 2 years ago

MyBatis version

3.5.9

Database vendor and version

mysql 5.7

Test case or example project

Steps to reproduce

  1. maven include mybatis
  2. package to a jar
  3. build with gralde

    /opt/graalvm-ce-java17-21.3.0/bin/native-image \
    --verbose --no-fallback -cp ./xxx-fat.jar \
    -H:Name=websocket -H:Class=xxx.xxx.Application -H:+ReportUnsupportedElementsAtRuntime \
    -H:+ReportExceptionStackTraces --allow-incomplete-classpath --enable-url-protocols=http \
    -H:+PrintClassInitialization \
    -J--add-exports=java.management/sun.management=ALL-UNNAMED \
    --trace-class-initialization=org.apache.ibatis.type.JdbcType

    Expected result

    build success

    Actual result

    com.oracle.svm.core.util.UserError$UserException: Classes that should be initialized at run time got initialized during image building: org.apache.ibatis.type.JdbcType was unintentionally initialized at build time. org.apache.ibatis.type.JdbcType caused initialization of this class with the following trace: at org.apache.ibatis.type.JdbcType.(JdbcType.java:30) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Unknown Source) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:568) at java.lang.Class.getEnumConstantsShared(Class.java:3837) at java.lang.Class.enumConstantDirectory(Class.java:3859) at java.lang.Enum.valueOf(Enum.java:267) at sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:479) at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:344) at java.lang.reflect.Method.getDefaultValue(Method.java:717) at sun.reflect.annotation.AnnotationType.(AnnotationType.java:133) at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85) at com.oracle.svm.core.code.MethodMetadataEncoder.prepareMetadataForClass(MethodMetadataEncoder.java:107) at com.oracle.svm.core.code.CodeInfoEncoder.prepareMetadataForClass(CodeInfoEncoder.java:240) at com.oracle.svm.hosted.image.NativeImageCodeCache.buildRuntimeMetadata(NativeImageCodeCache.java:230) at com.oracle.svm.hosted.image.LIRNativeImageCodeCache.layoutMethods(LIRNativeImageCodeCache.java:106) at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:608) at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:488) at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:403) at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:569) at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:122) at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:599)

    at com.oracle.svm.core.util.UserError.abort(UserError.java:73) at com.oracle.svm.hosted.classinitialization.ConfigurableClassInitialization.checkDelayedInitialization(ConfigurableClassInitialization.java:555) at com.oracle.svm.hosted.classinitialization.ClassInitializationFeature.afterImageWrite(ClassInitializationFeature.java:316) at com.oracle.svm.hosted.NativeImageGenerator.lambda$doRun$8(NativeImageGenerator.java:663) at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:73) at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:663) at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:488) at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:403) at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:569) at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:122) at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:599)

TruthBean commented 2 years ago

solution: change org.apache.ibatis.type.JdbcType.codeLookup to delayed filling

private final static Map<Integer,JdbcType> codeLookup = new HashMap<>();

  JdbcType(int code) {
    this.TYPE_CODE = code;
  }

  public static JdbcType forCode(int code)  {
    if (codeLookup.isEmpty()) {
      for (JdbcType type : JdbcType.values()) {
        codeLookup.put(type.TYPE_CODE, type);
      }
    }
    return codeLookup.get(code);
  }
linghengqian commented 2 years ago

Mybatis provides support for different modules in both Spring Native and Quarkus, and I don't think the corresponding GraalVM json file should be added to mybatis itself.

xuxiaowei-com-cn commented 3 months ago

:sparkles: Support MyBatis for Spring Boot 3 Native