X

Break New Ground

  • May 5, 2020

Adding the Oracle JDBC drivers and Oracle UCP to a Java project

Back in September 15th, 2019 (right before Oracle Code One and Oracle OpenWorld took place) Director of Product Management Kuassi Mensah posted an announcement that took many by surprise but was warmly welcomed: the Oracle JDBC drivers were finally available from Maven Central. This move allows developers to configure their applications to consume the Oracle JDBC drivers in the same way as they do for other 3rd party dependencies. Let's move forward to February 2020 where another announcement regarding these artifacts was made, in this case, a renaming of the base groupId which is now com.oracle.database.jdbc. One more thing that also became available in these announcements is Oracle UCP (Universal Connection Pool), the preferred way to pool connections to Oracle databases.

In this post and the ones to follow, I'll demonstrate how these technologies can be configured with popular frameworks of choice. Let's begin with the simplest choice which is direct JDBC access without a framework or ORM.

There are multiple ways to configure and build projects in the Java space, however, Apache Maven or Gradle are the popular choices. If your choice of tool is Apache Maven then you only have to add the following dependencies to your project:

However, if your choice is Gradle then you must make sure that dependencies can be resolved from Maven Central besides adding the required dependencies. The following snippet shows the minimum configuration:

Of course, if your company has a proxy or mirror for Maven Central then you'll likely have to follow your IT department's instructions to configure said server. The next step is to verify that the settings are working. For the case of Apache Maven this is as straight forward as invoking this command

mvn verify

This command will build the project, even if there were no source code at this point. You should see Apache Maven resolving dependencies and downloading the required artifacts. We're good to go if no errors appear on the console. If there were, it's likely that your build has stumbled upon a problem with firewalls, proxies, or perhaps the artifact mirror. Consult with your IT department if this were to be the case. For Gradle we can check if dependencies are configured correctly by invoking either of these two commands

gradle dependencies
gradle classes

Similarly, as it happened for Apache Maven, these commands will attempt resolving dependencies from the configured repositories. If all goes well no error should appear on the console. If there were a failure in resolving dependencies then the likely cause would be again firewall, proxies or the artifact mirror. Check with your IT department to solve this issue.

Once we have verified that dependencies can be resolved we can move to the code. For normal JDBC usage you typically require an instance of javax.sql.DataSource from which connections will be obtained. This is where UCP comes in. The documentation for UCP has examples on how to get started, configure the pool, troubleshooting in case of problems, and more. Follow the instructions in the Getting Started chapter to get a UCP DataSource configured and running. From here on forward, you may continue with standard JDBC code, that is, borrow connections from the pool, create statements, execute queries, and so forth. If you feel like it, you may combine UCP with lightweight database access libraries such as Sql2o, Jdbi, JOOQ, or similar.

Conclusion

It's now easier to build projects that required access to Oracle databases as the Oracle drivers are readily available from Maven Central. Configuring the drivers and UCP in a project built with either Apache Maven or Gradle is a straight forward task.

In the next installment, I'll discuss how to get the JDBC drivers configured with popular microservices and web app frameworks.

Credits:
Photo by Gerd Altmann

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.