• 24-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing [Another] Unable to acquire JDBC connection in Quarkusio Quarkus

[Another] Unable to acquire JDBC connection in Quarkusio Quarkus

Lightrun Team
Lightrun Team
24-Jan-2023

Explanation of the problem

The issue is that when making a large number of requests (1000-2000) in a short time period using Quarkus 1.4.2 Final, an error is encountered stating “Unable to acquire JDBC connection.” This occurs when using both Mariadb and Postgres as the datasource and the idle_in_transaction_session_timeout and statement_timeout have been set to 0.

The error occurs intermittently, sometimes at the 50th request and other times at the 200th request. The expected behavior is for the error to not occur. The steps to reproduce the behavior include: making a JDBC connection to Mariadb or Postgres, creating a JAX-RS GET path, creating a service class annotated with @RequestScoped, making a findAll call on a Panache entity within the service class, returning the list in the JAX-RS GET path, launching the application, and making many requests in a short time (using a bot or similar tool).

The attached configuration file includes the relevant Quarkus datasource properties, such as the connection URL, driver, username, and password. Screenshots of the error message are also provided.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for [Another] Unable to acquire JDBC connection in Quarkusio Quarkus

One potential solution for the “Unable to acquire JDBC connection” error in Quarkus is to increase the maximum number of connections in the connection pool. This can be done by adjusting the “quarkus.datasource.jdbc.max-size” property in the application.properties file. Additionally, you can also increase the maximum lifetime of a connection in the pool by adjusting the “quarkus.datasource.jdbc.max-lifetime” property, which can help prevent connections from being closed prematurely.

Another solution is to check if the connection pool is being exhausted by too many requests, this can be done by monitoring the connection pool usage using metrics, you can use the built-in metrics provided by quarkus or use other monitoring tools like Prometheus, Grafana, etc.

Another solution may be to increase the acquisition timeout setting, which can be done by adjusting the “quarkus.datasource.jdbc.acquisition-timeout” property.

Additionally, you should look into the idle removal interval, that can be done by adjusting the “quarkus.datasource.jdbc.idle-removal-interval” property, this will help to remove idle connections from the connection pool and free up resources.

It’s also important to review your code to ensure that you are properly closing connections and releasing resources when they are no longer needed.

It’s also important to check that your database is not overloading, you can check the number of max_connections in your db, if the number of connections is too high you can increase the number of max_connections.

It is also recommended to check your database logs to see if there is any indication of a problem.

It’s also important to check if the problem is not related to the max_connection limit in your database.

Other popular problems with Quarkusio Quarkus

Problem: “Unable to acquire JDBC connection” error

This error is encountered when making a large number of requests in a short time period using Quarkus, and occurs when the idle_in_transaction_session_timeout and statement_timeout have been set to 0. The error occurs intermittently, sometimes at the 50th request and other times at the 200th request.

Solution:

One potential solution for this problem is to increase the maximum number of connections in the connection pool by adjusting the “quarkus.datasource.jdbc.max-size” property in the application.properties file. Additionally, you can increase the maximum lifetime of a connection in the pool by adjusting the “quarkus.datasource.jdbc.max-lifetime” property, which can help prevent connections from being closed prematurely. Another solution is to check if the connection pool is being exhausted by too many requests, this can be done by monitoring the connection pool usage using metrics, you can use the built-in metrics provided by quarkus or use other monitoring tools like Prometheus, Grafana, etc.

Problem: “OutOfMemoryError: GC overhead limit exceeded” error

This error is encountered when the application is running for an extended period of time and the Java Virtual Machine (JVM) is unable to allocate memory for new objects. This can occur when the heap size is not properly configured or when the application is creating too many objects that are not being properly garbage collected.

Solution:

One solution for this problem is to increase the heap size by adjusting the -Xmx and -Xms JVM options. Another solution is to analyze the application’s memory usage using tools like VisualVM or jconsole to determine which objects are causing the memory leak and address them. Additionally, you can also use the -XX:+HeapDumpOnOutOfMemoryError JVM option to dump the heap when the OutOfMemoryError occurs, and use a heap analyzer to identify the root cause of the problem.

Problem: High CPU usage

High CPU usage can occur when the application is performing a large number of computations or when the application is stuck in an infinite loop. This can cause the application to become unresponsive and affect the overall performance.

Solution:

One solution for this problem is to use a profiler such as VisualVM or jprofiler to identify the source of the high CPU usage and optimize the code accordingly. Another solution is to use a monitoring tool like Prometheus, to monitor the application’s resource usage, and identify the source of high CPU usage. Additionally, you can also use the -XX:+PrintCompilation JVM option to print the compilation details of the JIT compiler, which can help identify the hotspot methods that are causing the high CPU usage.

A brief introduction to Quarkusio Quarkus

Quarkus is a Kubernetes-native Java stack tailored for GraalVM and OpenJDK HotSpot. It is designed to make Java a leading platform in Kubernetes and cloud-native environments while offering developer productivity and fast boot time. Quarkus supports both imperative and reactive programming models, and it allows developers to build applications with a small memory footprint and fast startup time.

Quarkus provides a set of extensions that allow developers to easily add functionality to their applications, such as database access, security, and messaging. These extensions are built on top of popular Java frameworks and libraries, such as Eclipse MicroProfile, Hibernate, and Apache Camel. Quarkus also integrates with popular build tools such as Maven and Gradle, making it easy to use in existing Java projects. Additionally, Quarkus provides a development mode that allows developers to hot-reload changes to their code without having to restart the application, allowing for a faster development experience.

Most popular use cases for Quarkusio Quarkus

  1. Quarkus is a Kubernetes-native Java framework that allows for the development of efficient, lightweight, and fast-starting Java applications. It utilizes a “supersonic, subatomic” Java stack that can run on a variety of platforms, including OpenJDK, GraalVM, and native executables.
  2. Quarkus provides a set of features and extensions that enable developers to build Java applications with a smaller memory footprint, faster startup times, and lower resource usage. This is achieved through the use of Ahead-of-Time (AOT) compilation and the use of GraalVM’s native image capabilities.
  3. Quarkus also allows for the use of Java code in a functional programming style, using the extension of SmallRye Functional . This allows for developers to write code using functional constructs such as lambdas and streams, and is useful for writing concise, expressive, and maintainable code.
import org.eclipse.microprofile.function.FunctionConfig;
import org.eclipse.microprofile.function.FunctionConfig.Builder;

@FunctionConfig(name = "myfunction")
public class MyFunction {
    public String greet(String name) {
        return "Hello " + name;
    }
}
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.