MyBatis version
3.5.9
Database vendor and version
mysql 5.7
Test case or example project
Steps to reproduce
- maven include mybatis
- package to a jar
- 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.
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)
Comment From: TruthBean
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);
}
Comment From: linghengqian
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.
Comment From: xuxiaowei-com-cn