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

Provider org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl could not be instantiated GraalVm #2976

Closed
LukaszBaryczka opened this issue Jun 7, 2024 · 5 comments

Comments

@LukaszBaryczka
Copy link

Expected Behavior

I'm trying to create simple CRUD application and I based on this wiki https://guides.micronaut.io/latest/micronaut-jpa-hibernate-maven-java.html. My expectation is that I will be able to make micronaut application in the latest version with postgres db with simple REST API in native mode with graalvm.

Actual Behaviour

Everythink is working correctly in jvm mode, but when I try to start it in native mode with graalvm I notice the problem:

api-1           | 12:41:17.988 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.SessionFactory]
api-1           | 
api-1           | Message: org.hibernate.bytecode.spi.BytecodeProvider: Provider org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl could not be instantiated

I tried to fix this problem based on the description and comments from #2462 issue, but it is still not working.

My pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.1</version>
  <packaging>${packaging}</packaging>

  <parent>
    <groupId>io.micronaut.platform</groupId>
    <artifactId>micronaut-parent</artifactId>
    <version>4.4.3</version>
  </parent>
  <properties>
    <packaging>jar</packaging>
    <jdk.version>21</jdk.version>
    <release.version>21</release.version>
    <micronaut.version>4.4.3</micronaut.version>
    <micronaut.aot.enabled>false</micronaut.aot.enabled>
    <micronaut.aot.packageName>com.example.aot.generated</micronaut.aot.packageName>
    <micronaut.runtime>netty</micronaut.runtime>
    <exec.mainClass>com.example.Application</exec.mainClass>
  </properties>

  <repositories>
    <repository>
      <id>central</id>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>io.micronaut</groupId>
      <artifactId>micronaut-http-server-netty</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut.data</groupId>
      <artifactId>micronaut-data-hibernate-jpa</artifactId>
      <scope>compile</scope>
    </dependency>
<!--    <dependency>-->
<!--      <groupId>io.micronaut.data</groupId>-->
<!--      <artifactId>micronaut-data-tx-hibernate</artifactId>-->
<!--      <scope>compile</scope>-->
<!--    </dependency>-->
    <dependency>
      <groupId>io.micronaut.serde</groupId>
      <artifactId>micronaut-serde-jackson</artifactId>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.micronaut.sql</groupId>
      <artifactId>micronaut-hibernate-jpa</artifactId>
      <scope>compile</scope>
    </dependency>
<!--    <dependency>-->
<!--      <groupId>io.micronaut.sql</groupId>-->
<!--      <artifactId>micronaut-jdbc-hikari</artifactId>-->
<!--      <scope>compile</scope>-->
<!--    </dependency>-->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <scope>runtime</scope>
    </dependency>
<!--    <dependency>-->
<!--      <groupId>org.projectlombok</groupId>-->
<!--      <artifactId>lombok</artifactId>-->
<!--      <scope>provided</scope>-->
<!--    </dependency>-->
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>io.micronaut.maven</groupId>
        <artifactId>micronaut-maven-plugin</artifactId>
        <configuration>
          <configFile>aot-${packaging}.properties</configFile>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <!-- Uncomment to enable incremental compilation -->
          <!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->

          <annotationProcessorPaths combine.self="override">
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>${lombok.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-inject-java</artifactId>
              <version>${micronaut.core.version}</version>
            </path>
            <path>
              <groupId>io.micronaut.data</groupId>
              <artifactId>micronaut-data-processor</artifactId>
              <version>${micronaut.data.version}</version>
              <exclusions>
                <exclusion>
                  <groupId>io.micronaut</groupId>
                  <artifactId>micronaut-inject</artifactId>
                </exclusion>
              </exclusions>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-graal</artifactId>
              <version>${micronaut.core.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-http-validation</artifactId>
              <version>${micronaut.core.version}</version>
            </path>
            <path>
              <groupId>io.micronaut.serde</groupId>
              <artifactId>micronaut-serde-processor</artifactId>
              <version>${micronaut.serialization.version}</version>
              <exclusions>
                <exclusion>
                  <groupId>io.micronaut</groupId>
                  <artifactId>micronaut-inject</artifactId>
                </exclusion>
              </exclusions>
            </path>
          </annotationProcessorPaths>
          <compilerArgs>
            <arg>-Amicronaut.processing.group=com.example</arg>
            <arg>-Amicronaut.processing.module=demo</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

I found that similar problem was noticed with spring boot and hibernate 6.0(spring-projects/spring-boot#39744) and recomendation was to downgrade the hibernate, so the question is: Is it supported in micronaut to create an application with connection to database in micronaut 4.4.3 in native mode? Could you help me or provide some docs where micronaut + JPA/JDBC + graalvm will be described?

Steps To Reproduce

  1. mvn clean package -Dpackaging=native-image
  2. docker-compose -f docker-compose.native.yml up

Environment Information

OS: Ubuntu 22.04
Java:

java version "21.0.3" 2024-04-16 LTS
Java(TM) SE Runtime Environment Oracle GraalVM 21.0.3+7.1 (build 21.0.3+7-LTS-jvmci-23.1-b37)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.3+7.1 (build 21.0.3+7-LTS-jvmci-23.1-b37, mixed mode, sharing)

Example Application

https://github.com/LukaszBaryczka/micronaut-db-native

Version

4.4.3

@radovanradic
Copy link
Contributor

Don't know if you changed it temporarily in your repo, but this configuration should be true

jpa:
  default:
    compile-time-hibernate-proxies: true

and need to add this dependency to pom.xml

<dependency>
      <groupId>io.micronaut.sql</groupId>
      <artifactId>micronaut-jdbc-hikari</artifactId>
      <scope>compile</scope>
    </dependency>

LukaszBaryczka pushed a commit to LukaszBaryczka/micronaut-db-native that referenced this issue Jun 7, 2024
@LukaszBaryczka
Copy link
Author

You right - I change it temporairly bcs I tried to make some experiments with dependency and properies which I found it other threats :) I change it on the repo already.

Unfortunatelly error is the same, so it not fix the main problem which was describe by me :(

@LukaszBaryczka
Copy link
Author

Maybe it will be good idea to add full startup logs with stack trace from the application:

api-1           |  __  __ _                                  _   
api-1           | |  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
api-1           | | |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
api-1           | | |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
api-1           | |_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
api-1           | 14:15:24.077 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
api-1           | 14:15:24.093 [main] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@6a14999a
api-1           | 14:15:24.093 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
api-1           | 14:15:24.097 [main] INFO  org.hibernate.Version - HHH000412: Hibernate ORM core version 6.4.4.Final
api-1           | 14:15:24.099 [main] INFO  o.h.c.i.RegionFactoryInitiator - HHH000026: Second-level cache disabled
api-1           | 14:15:24.109 [main] WARN  org.hibernate.orm.incubating - HHH90006001: Encountered incubating setting [hibernate.id.db_structure_naming_strategy].  See javadoc on corresponding `org.hibernate.cfg.AvailableSettings` constant for details.
api-1           | 14:15:24.109 [main] WARN  org.hibernate.orm.incubating - HHH90006001: Encountered incubating setting [hibernate.id.db_structure_naming_strategy].  See javadoc on corresponding `org.hibernate.cfg.AvailableSettings` constant for details.
api-1           | 14:15:24.109 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.SessionFactory]
api-1           | 
api-1           | Message: org.hibernate.bytecode.spi.BytecodeProvider: Provider org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl could not be instantiated
api-1           | Path Taken: SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder([SessionFactoryBuilder sessionFactoryBuilder])
api-1           | io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.SessionFactory]
api-1           | 
api-1           | Message: org.hibernate.bytecode.spi.BytecodeProvider: Provider org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl could not be instantiated
api-1           | Path Taken: SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder([SessionFactoryBuilder sessionFactoryBuilder])
api-1           |       at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1982)
api-1           |       at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:294)
api-1           |       at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3348)
api-1           |       at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3692)
api-1           |       at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:342)
api-1           |       at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:202)
api-1           |       at io.micronaut.runtime.Micronaut.start(Micronaut.java:74)
api-1           |       at io.micronaut.runtime.Micronaut.run(Micronaut.java:328)
api-1           |       at io.micronaut.runtime.Micronaut.run(Micronaut.java:314)
api-1           |       at com.example.Application.main(Application.java:8)
api-1           |       at [email protected]/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)
api-1           | Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [org.hibernate.SessionFactory]
api-1           | 
api-1           | Message: org.hibernate.bytecode.spi.BytecodeProvider: Provider org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl could not be instantiated
api-1           | Path Taken: SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder([SessionFactoryBuilder sessionFactoryBuilder])
api-1           |       at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2326)
api-1           |       at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2281)
api-1           |       at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2293)
api-1           |       at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3095)
api-1           |       at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
api-1           |       at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2997)
api-1           |       at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2958)
api-1           |       at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2732)
api-1           |       at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1731)
api-1           |       at io.micronaut.context.AbstractBeanResolutionContext.getBean(AbstractBeanResolutionContext.java:89)
api-1           |       at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:2161)
api-1           |       at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanForConstructorArgument(AbstractInitializableBeanDefinition.java:1324)
api-1           |       at io.micronaut.configuration.hibernate.jpa.conf.$SessionFactoryPerDataSourceFactory$BuildHibernateSessionFactoryBuilder4$Definition.instantiate(Unknown Source)
api-1           |       at io.micronaut.context.BeanDefinitionDelegate.instantiate(BeanDefinitionDelegate.java:159)
api-1           |       at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2311)
api-1           |       at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2281)
api-1           |       at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2293)
api-1           |       at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3095)
api-1           |       at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
api-1           |       at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2997)
api-1           |       at io.micronaut.context.DefaultBeanContext.initializeEagerBean(DefaultBeanContext.java:2669)
api-1           |       at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1976)
api-1           |       ... 10 common frames omitted
api-1           | Caused by: java.util.ServiceConfigurationError: org.hibernate.bytecode.spi.BytecodeProvider: Provider org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl could not be instantiated
api-1           |       at [email protected]/java.util.ServiceLoader.fail(ServiceLoader.java:586)
api-1           |       at [email protected]/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:813)
api-1           |       at [email protected]/java.util.ServiceLoader$ProviderImpl.get(ServiceLoader.java:729)
api-1           |       at org.hibernate.boot.registry.classloading.internal.AggregatedServiceLoader$ClassPathAndModulePathAggregatedServiceLoader.collectServiceIfNotDuplicate(AggregatedServiceLoader.java:184)
api-1           |       at org.hibernate.boot.registry.classloading.internal.AggregatedServiceLoader$ClassPathAndModulePathAggregatedServiceLoader.loadAll(AggregatedServiceLoader.java:125)
api-1           |       at org.hibernate.boot.registry.classloading.internal.AggregatedServiceLoader$ClassPathAndModulePathAggregatedServiceLoader.getAll(AggregatedServiceLoader.java:112)
api-1           |       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:241)
api-1           |       at org.hibernate.bytecode.internal.BytecodeProviderInitiator.initiateService(BytecodeProviderInitiator.java:50)
api-1           |       at org.hibernate.bytecode.internal.BytecodeProviderInitiator.initiateService(BytecodeProviderInitiator.java:22)
api-1           |       at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:130)
api-1           |       at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
api-1           |       at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:238)
api-1           |       at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215)
api-1           |       at org.hibernate.boot.internal.SessionFactoryBuilderImpl.<init>(SessionFactoryBuilderImpl.java:69)
api-1           |       at org.hibernate.boot.internal.SessionFactoryBuilderImpl.<init>(SessionFactoryBuilderImpl.java:46)
api-1           |       at org.hibernate.boot.internal.DefaultSessionFactoryBuilderService.createSessionFactoryBuilder(DefaultSessionFactoryBuilderService.java:26)
api-1           |       at org.hibernate.boot.internal.MetadataImpl.getSessionFactoryBuilder(MetadataImpl.java:170)
api-1           |       at io.micronaut.configuration.hibernate.jpa.conf.AbstractHibernateFactory.buildHibernateSessionFactoryBuilder(AbstractHibernateFactory.java:96)
api-1           |       at io.micronaut.configuration.hibernate.jpa.conf.SessionFactoryPerDataSourceFactory.buildHibernateSessionFactoryBuilder(SessionFactoryPerDataSourceFactory.java:100)
api-1           |       at io.micronaut.configuration.hibernate.jpa.conf.$SessionFactoryPerDataSourceFactory$BuildHibernateSessionFactoryBuilder3$Definition.doInstantiate(Unknown Source)
api-1           |       at io.micronaut.context.AbstractInitializableBeanDefinition.instantiate(AbstractInitializableBeanDefinition.java:770)
api-1           |       at io.micronaut.context.BeanDefinitionDelegate.instantiate(BeanDefinitionDelegate.java:156)
api-1           |       at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2311)
api-1           |       ... 31 common frames omitted
api-1           | Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: No classes have been predefined during the image build to load from bytecodes at runtime.
api-1           |       at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:121)
api-1           |       at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.PredefinedClassesSupport.throwNoBytecodeClasses(PredefinedClassesSupport.java:76)
api-1           |       at org.graalvm.nativeimage.builder/com.oracle.svm.core.hub.PredefinedClassesSupport.loadClass(PredefinedClassesSupport.java:130)
api-1           |       at [email protected]/java.lang.ClassLoader.defineClass(ClassLoader.java:280)
api-1           |       at net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader.invoker(JavaDispatcher.java:1383)
api-1           |       at net.bytebuddy.utility.dispatcher.JavaDispatcher$InvokerCreationAction.run(JavaDispatcher.java:459)
api-1           |       at net.bytebuddy.utility.dispatcher.JavaDispatcher$InvokerCreationAction.run(JavaDispatcher.java:452)
api-1           |       at [email protected]/java.security.AccessController.executePrivileged(AccessController.java:129)
api-1           |       at [email protected]/java.security.AccessController.doPrivileged(AccessController.java:319)
api-1           |       at net.bytebuddy.utility.dispatcher.JavaDispatcher.doPrivileged(JavaDispatcher.java)
api-1           |       at net.bytebuddy.utility.dispatcher.JavaDispatcher.<clinit>(JavaDispatcher.java:87)
api-1           |       at net.bytebuddy.description.type.TypeDescription$ForLoadedType.<clinit>(TypeDescription.java:8659)
api-1           |       at net.bytebuddy.matcher.ElementMatchers.isFinalizer(ElementMatchers.java:1624)
api-1           |       at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState$ProxyDefinitionHelpers.<init>(ByteBuddyState.java:296)
api-1           |       at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.<clinit>(ByteBuddyState.java:71)
api-1           |       at org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl.<init>(BytecodeProviderImpl.java:124)
api-1           |       at org.hibernate.bytecode.internal.bytebuddy.BytecodeProviderImpl.<init>(BytecodeProviderImpl.java:116)
api-1           |       at [email protected]/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
api-1           |       at [email protected]/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
api-1           |       at [email protected]/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:789)
api-1           |       ... 52 common frames omitted
api-1 exited with code 1

@radovanradic
Copy link
Contributor

Can you try without docker (I was getting some errors when running docker compose so tested this way)

./mvnw package -Dpackaging=native-image
./target/demo

@LukaszBaryczka
Copy link
Author

The error which you noticed was related with the fact that this app needs db to startup. With local postgres and changed db url in properties file, this app start working correctly. But this fact was a surprise for me, and after it I change a focuse from the app to the dockerfile :)

My main problem is also fixed. Finally it was a problem with dockerfile for native -> I did not copy autogenerated libs for native from target...
Corrected dockerfile:

FROM cgr.dev/chainguard/wolfi-base:latest
COPY target/demo /app/demo
COPY target/lib* /app/
RUN chmod +x /app/demo
EXPOSE 8080
ENTRYPOINT ["/app/demo"]

when I add

COPY target/lib* /app/

line it start working.
Thx for help, and I will close this issue.

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

No branches or pull requests

2 participants