Skip to content

chiroito/Jfr4Jdbc

Repository files navigation

Introduction

Jfr4Jdbc is a JDBC wrapper library that can be used with any type of database server. Jfr4Jdbc makes it easy for users to determine whether a system performance problem is occurring in the Java application or in the database.

Jfr4Jdbc records operations on JDBC as events in the JDK Flight Recorder (JFR). The load on this logging is slight. Users can dump the JFR and open the dump file with JDK Mission Control to see the database operations in the GUI. This allows the user to graphically analyze all database processing problems.

In a JVM with JFR enabled, you can benefit by simply adding this as a library and adding jfr: to your jdbc configurations. Using this driver, you can obtain information about your database access, such as connection , statement, commit etc., as an JFR events.

Fine-grained control via API is also supported. For example, if you want to analyze only some database connections, you can wrap the JDBC objects, such as DataSource or Connection, you want to analyze with the JDBC objects provided by Jfr4Jdbc, such as Jfr4jdbcDataSource.

What you can see

You will be able to see which threads are processing to the database and when. This feature allows you to visually determine which operations on the database, such as connecting, executing statements, committing, etc., are taking the most time.

Thread Lane

You can analyze how many connections are in use and how many threads are stuck waiting for connection assignments. Connections belonging to data sources are grouped by data source. You can identify which connection pools are missing and which databases are taking a long time to connect to. Usage

Recorded Event

Each time you use the API to access the database, you will get the following information

  • Getting connection from DataSource
  • Establish a connection to the Database
  • Closing connection
  • Executing statement
  • Fetching ResultSet
  • Commit
  • Rollback
  • Cancel

Additionally, you can also get the following numbers every second by default.

  • connections being used
  • connections waiting to be assigned

Supported Environment

  • JDBC 4.2 with OpenJDK 8 update 272
  • JDBC 4.3 with OpenJDK 11+

Closed JDKs such as Oracle JDK are not supported.

How to download

You can download Jfr4Jdbc as a JAR file from Maven Central

Those who use build automation tools such as Maven or Gradle to manage dependencies should add the following to their dependencies

Maven

<dependency>
  <groupId>dev.jfr4jdbc</groupId>
  <artifactId>jfr4jdbc-driver</artifactId>
  <version>1.3.0</version>
</dependency>

Gradle

implementation 'dev.jfr4jdbc:jfr4jdbc-driver:1.3.0'

Scala SBT

libraryDependencies += "dev.jfr4jdbc" % "jfr4jdbc-driver" % "1.3.0"

Bazel

maven_jar(
    name = "jfr4jdbc-driver",
    artifact = "dev.jfr4jdbc:jfr4jdbc-driver:1.3.0",
    sha1 = "669dd4fa8797e04a79ab5e13e29d646e2863d76d",
)

How to use

Jfr4Jdbc can be integrated using the API described below, but the easiest way is to add jfr: to the JDBC configuration.

Those using Quarkus or Spring Boot as application frameworks should add jfr: as follows

Quarkus

quarkus.datasource.jdbc.url=jdbc:jfr:postgresql://localhost:5432/xxx

Spring Boot

spring.datasource.url=jdbc:jfr:postgresql://postgres:5432/xxx

How to use in code

The API allows more fine-grained control over the events to be captured.

Use with javax.sql.DataSource

You can pass an instance of javax.sql.DataSource to the constructor argument of Jfr4JdbcDataSource to record events for all connections managed by this data source and all statements generated by it.

OracleDataSource ds = new OracleDataSource();  
ds.setURL("jdbc:oracle:thin:user/passwd@localhost:1521:XE");  
Jfr4jdbcDataSource jds = new Jfr4jdbcDataSource(ds);  
Connection con = jds.getConnection();

Use with java.sql.Driver

Add the "jfr:" in the back of "jdbc:" of the JDBC connection string. You can record events for all statements generated by this connection.

String url = "jdbc:jfr:oracle:thin:user/passwd@localhost:1521:XE";  
Driver driver = DriverManager.getDriver(url);  
Connection con = driver.connect(url, null);