Introduction to oracle jdbc driver support for virtual threads

Introduction to oracle jdbc driver support for virtual threads


Play all audios:

Loading...

by Juarez Junior INTRODUCTION This blog post introduces Oracle’s JDBC Driver support for Virtual Threads. Oracle has built its JDBC Driver from version 21c and newer versions to support


synchronous JDBC access using virtual (lightweight) threads. This step-by-step guide is not an extensive introduction to Virtual Threads but a straightforward guide on utilizing Oracle’s


JDBC support to Virtual Threads with the JDBC API and synchronous (blocking calls) specifically. Given that, it’s time to get started! A QUICK INTRODUCTION TO PROJECT LOOM AND JAVA VIRTUAL


THREADS Project Loom is an umbrella project intended to define, incubate and deliver Java VM features to support easy-to-use, high-throughput lightweight concurrency and new programming


models on the Java platform. Per the JDK release process, features in Project Loom were broken down into several JEPs (JDK Enhancement Proposals) and made available in different JDK


releases. A targeted feature is the addition of Virtual Threads to the Java platform. Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and


observing high-throughput concurrent applications. JDK 19 included Virtual Threads as a preview API. If you need an introduction to Virtual Threads, please check the blog posts below: *


Coming to Java 19: Virtual threads and platform threads * Going inside Java’s Project Loom and virtual threads WHY SHOULD JAVA AND DATABASE DEVELOPERS CARE ABOUT VIRTUAL THREADS? To leverage


improved resource efficiency and better processing throughput benefits, developers don’t have to refactor simple blocking JDBC calls into Reactive Streams calls that are hard to create,


maintain and debug anymore. Our sample source code now uses Java Virtual Threads with the JDBC API, as it demonstrates a new approach. It will help your applications leverage your


infrastructure-related resources and capacity in an optimized way, avoiding the classic java.lang.OutOfMemoryError issue and reducing hardware utilization in terms of memory, disk, and


processor time, as examples. Below is a summary of the benefits and limitations of Virtual Threads. The main advantage is leveraging your underlying computing resources more efficiently, as


you can run millions of threads compared to traditional Java Platform threads. BENEFITS OF VIRTUAL THREADS: * Easier to read and write * Easier to adopt (little or no code change) * Easier


to debug * Integration with JDK tools LIMITATIONS OF VIRTUAL THREADS: * Still a preview feature in JDK 19 * Some libraries are not compatible yet * Synchronized blocks/method cause thread


pinning THE JAVA CODE SAMPLE Below are some code snippets illustrating Virtual Threads with the JDBC API. The first code sample —_PlatformThreadsOracleJdbc.java_ — uses a classic Java


Platform (carrier) thread to perform a database query with JDBC. As we can see below, the default constructor for java.lang.Thread is called. The second code sample —


_VirtualThreadsOracleJdbc.java_ — uses a Virtual Thread to perform a database query with JDBC. We can see below that a different method instantiates a Virtual Thread — public static Thread


startVirtualThread(Runnable task). Concerning both examples, a simple query statement is used for illustrational purposes only as required. SELECT * FROM SH.CUSTOMERS WHERE CUST_ID = 49671


RUN THE SAMPLE APPLICATION Now let’s run the sample code. Open the first Java class, _VirtualThreadsOracleJdbc.java_. Select the Run icon below, then Run As -> Java Application. With


Virtual Threads, the number of kernel threads remains low and stable, as highlighted below, as the JVM uses the underlying resources more efficiently. Now, open the second Java class,


_PlatformThreadsOracleJdbc.java_. Select the Run icon below, then Run As -> Java Application. With Platform Threads, OS kernel threads increase considerably as the JVM maps each platform


(carrier) thread to a corresponding kernel thread regarding the underlying OS-related resources. CONFIGURE JAVA 19 SUPPORT Remember that a specific feature switch option has to be enabled to


use Virtual Threads. Open Eclipse, then select Window -> Preferences -> Java -> Compiler. Select the checkboxes for Java 19 support (preview features) as below. You can also use a


command-line option if you prefer. Just follow the instructions available at Java SE 19 -> Preview Features. A sample Maven POM.xml file is also pre-configured for the sample project to


support the use of preview features, as shown below. PROVISIONING AN ORACLE DATABASE Our scenario with the JDBC Driver and Virtual Threads uses an Oracle ATP database. There are many options


to provision an ATP instance, including the OCI console, the OCI SDKs, and the Oracle Database Operator for Kubernetes. The guides below can help you with this task, depending on your


choice. * Getting Started with Java/JDBC and Oracle Autonomous Database * Oracle Database XE for Students and Developers on Windows 11 In the scope of this tutorial, we have provisioned an


instance of Oracle Autonomous Transaction Processing (ATP) as below. CONFIGURE THE JDBC CONNECTION DETAILS Get the code sample. You must modify the config.properties file and provide the


required connection details. They are required to create a JDBC connection as usual. WRAPPING IT UP All right! Now you can keep writing JDBC-related code as usual, in a blocking but


resource-effective way. As explained, it’s easier to debug code that uses the standard JDBC API and Virtual Threads, which may simplify how we write, debug and maintain Java database


applications. I hope you enjoyed this tutorial! REFERENCES Developers Guide For Oracle JDBC 21c on Maven Central Oracle Database JDBC Java API Reference, Release 21c JEP 425: Virtual Threads


(Preview) JDK 19 Documentation Develop Java applications with Oracle Database The Java Tutorials: JDBC Basics Oracle JDBC FAQ JDBC 4.3 API JSR-000221 JDBCTM API 4.3 Maintenance Release 3


JSR 221: JDBCTM 4.0 API Specification Oracle Autonomous Database for Transaction Processing and Mixed Workloads ORACLE DEVELOPERS AND ORACLE OCI FREE TIER Join our Oracle Developers channel


on Slack to discuss Java, JDBC, Virtual Threads, and other topics! Build, test, and deploy your applications on Oracle Cloud — for free! Get access to OCI Cloud Free Tier!