Developing an Oracle JDBC app with GraalVM Native Image
Introduction
The Oracle JDBC Driver built-in configuration for GraalVM Native Image enables you to create GraalVM Native executables for applications that use the Oracle JDBC Driver to access an Oracle Database.
GraalVM native executables can deliver faster start-up times and run with minimal resource utilization and overhead, making them ideal for applications that require high performance and low latency.
Use the Oracle JDBC Driver with GraalVM Native Image
The Oracle JDBC Driver (from 21c) has been instrumented with GraalVM configuration available in META-INF/native-image.
So, to use the Oracle Database with GraalVM Native Image, you only have to add the Oracle JDBC Driver to your project’s classpath as usual.
An example with Maven is below for your reference.
<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>
...
</dependencies>
Nevertheless, the sample code already has it configured as a dependency for you.
Get the application (code sample)
The code sample is available on GitHub. As usual, you have to modify the database connection details as shown below:
Run the Oracle Database 23c Free — Developer Release as a Docker Container on Windows
As explained in my previous blog post, run the Oracle Database 23c Free — Developer Release as a Docker container on Windows.
I won’t go into all the details of that here again, if you need to check all the steps beyond the command below please check the related blog post mentioned above.
To do so, first, run the docker pull command below:
docker pull gvenzl/oracle-free
Now, you can run the docker images command below to confirm that it was pulled properly:
Then, run the command below from the Windows Command Prompt (cmd):
docker run -d -p 1521:1521 -e ORACLE_PASSWORD=<your password> -v oracle-volume:/opt/oracle/oradata gvenzl/oracle-free
Replace <your password> above with your chosen password as required. The command above will run a new persistent database container (data is kept throughout container lifecycles).
Note that you have also to prepare a directory called oradata to persist data with the oracle-volume as shown above.
If everything goes well, you can run the command docker ps -al to confirm that your container instance is up and running as expected.
At a later point, if you want to change/update the password, you can use the command below:
docker exec <container name|id> resetPassword <your password>
There are some additional options if you want to configure environment variables to support your connection details such as username, password, and other variables. Besides, you can check more details about the specific database user (SYS, SYSTEM, etc) you may want to use.
There are also details to run a new persistent database container, in which data is kept throughout container lifecycles.
Last, there are more details on how to use GitHub Actions as well. Please check the official landing page for the related Docker image on Docker Hub if you want to explore such options.
Now your database is installed, and up and running!
Run the application with a JVM as usual (Java JIT (C2) Compiler)
Run the Java class of your application by using Maven as shown below.
mvn clean package exec:java -Dexec.mainClass="com.oracle.jdbc.graalvm.GraalVMNativeImageJDBCDriver"
The application is just a simple one that performs a JDBC query against an Oracle Database 23c Free — Developer Release database instance.
You should see a query result similar to the screenshot below.
Native images with GraalVM
Given that, we’ll be able to use GraalVM to create a native image for our application.
I’m using GraalVM JDK 21 on Windows, so there are a few documented steps that you have to follow in order to make it work properly.
In a nutshell, you have to install the Visual Studio Build Tools. Do not forget that it has to match the details of your underlying hardware platform!
Then, use a specialized CMD prompt (shell) called x64 Native Tools Command Prompt when running commands from the CLI (CMD) that will require GraalVM and the native-image tool.
I won’t go into all the details of the previous installations as it’s vastly documented, so you can check the official technical resources provided by the GraalVM project.
Build the GraalVM native image
Now that you have the code sample with the application, it’s time to build the GraalVM native image for your application.
Let’s proceed to first build the GraalVM Native executable of our JDBC application by generating it, and then test the application again by running the native image executable file (.exe file on Windows).
Open the specific CMD prompt (x64 Native Tools Command Prompt as an example) as shown in the previous section.
Move to the project’s root where the Maven’s pom.xml file is located and run the the command below.
mvn -Pnative package
As soon as the native image creation completes the GraalVM Native Image will be available under the /target directory, and it will use the Oracle JDBC Driver to an Oracle Database instance.
The screenshot below shows the result of a successful build.
Run the Native Image of your Application
Run the GraalVM Native Image of your application. It should be available under the /target directory as below. Note that the JVM is no longer needed at this stage.
To execute the GraalVM Native Image of your application, just run the executable file (.exe) on Windows as shown below.
jdbc-driver-graalvm-nativeimage.exe
Provided that everything is OK, the application will start, connect to the Oracle Database and show the same JDBC query results as in the previous execution of a JVM-based (JIT) release of it.
Wrapping it up
You learned about the Oracle JDBC Driver built-in configuration for GraalVM Native Image. As discussed, a GraalVM native executable can deliver faster start-up times and run with minimal resource utilization and overhead, making them ideal for applications that require high performance and low latency. That’s it! I hope you enjoyed this blog post, so stay tuned!
References
Oracle Database 23c Free — Developer Release
Develop Java applications with Oracle Database
Oracle Developers and Oracle OCI Free Tier
Join our Oracle Developers channel on Slack to discuss Java, JDK, JDBC, GraalVM, Microservices with Spring Boot, Helidon, Quarkus, Micronaut, Reactive Streams, Cloud, DevOps, IaC, and other topics!
Build, test, and deploy your applications on Oracle Cloud — for free! Get access to OCI Cloud Free Tier!