Giter Club home page Giter Club logo

jfr-jdbctemplate's Introduction

JFR JdbcTemplate Maven Central Javadocs

An implementation of Spring JdbcTemplate that generates Flight Recorder events.

This project requires Java 11 based on OpenJDK or later.

<dependency>
  <groupId>com.github.marschall</groupId>
  <artifactId>jfr-jdbctemplate</artifactId>
  <version>2.0.0</version>
</dependency>

Versions 1.x are intended for Spring 5.x / Java 11, versions 2.x are intended for Spring 6.x / Java 17.

Flight Recording of a JUnit Test

Compared to approaches based on DataSource an approach based on JdbcTemplate has the advantage that it captures a complete database interaction. For example if you process many rows the initial PreparedStatement#execute() might be fast but most of the time may be spent in ResultSet#next(). A JdbcTemplate based approach generates a single JFR event for the entire interaction that involves several JDBC method invocations.

Spring Class JFR Class
org.springframework.jdbc.core.JdbcOperations com.github.marschall.jfr.jdbctemplate.JfrJdbcOperations
org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations com.github.marschall.jfr.jdbctemplate.JfrNamedParameterJdbcOperations

Reported Attributes

operationName
The name of the execute JDBC operation, this corresponds to the method name on JdbcOperations/JdbcTemplate.
query
The SQL query string passed to the JDBC driver. May be missing especially if custom org.springframework.jdbc.core.PreparedStatementCreator fail to implement org.springframework.jdbc.core.SqlProvider.
rowCount
In the case of a SELECT the number of rows returned. In the case of an UPDATE or DELETE the number of rows affected. -1 for a statement that does not return anything like a DDL. -2 when no information about the number of rows is available.

Overhead

We try to keep overhead to a minimum and have no additional allocations besides the JFR events. Besides the overhead of the event the only additional overhead is:

  • a wrapper around JdbcTemplate
  • a few instanceof operations and casts
  • a finally block
  • a capturing lambda for #queryForStream methods to record Stream#close as the end time of the event
  • a small wrapper around every RowCallbackHandler

We assume org.springframework.jdbc.core.SqlProvider#getSql() is a simple getter.

Usage

@Configuration
public class JdbcConfiguration {

   @Autowired
   private DataSource dataSource;

   @Bean
   public JdbcOperations jdbcOperations() {
     return new JfrJdbcOperations(new JdbcTemplate(this.dataSource));
   }

   @Bean
   public NamedParameterJdbcOperations namedParameterJdbcOperations() {
     return new JfrNamedParameterJdbcOperations(new NamedParameterJdbcTemplate(this.jdbcOperations()));
   }

}

You need something like the following JVM options to run Flight Recorder

-XX:StartFlightRecording:filename=recording.jfr
-XX:FlightRecorderOptions:stackdepth=128

Limitations

  • When the SQL query is not provided as a String but as a PreparedStatementCreator or CallableStatementCreator it has to implement SqlProvider for the query string to show up in the flight recording.
  • JdbcTemplate#query(PreparedStatementCreator, PreparedStatementSetter, ResultSetExtractor) is not available because it is defined on JdbcTemplate and not JdbcOperations.
  • Several spring-jdbc classes AbstractJdbcCall, SimpleJdbcCall, StoredProcedure, RdbmsOperation, AbstractJdbcInsert, SimpleJdbcInsert but also JdbcTestUtils and JdbcBeanDefinitionReader require a JdbcTemplate and do not work with JdbcOperations. We have a pull request open for this but it has not been merged yet.
  • JdbcOperations#execute(ConnectionCallback) can not provide any insight into what is executed inside, that would require integration with marschall/jfr-jdbc

jfr-jdbctemplate's People

Contributors

dependabot[bot] avatar marschall avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jfr-jdbctemplate's Issues

Command line in documentation can be simplified

The documentation reads:

-XX:+FlightRecorder
-XX:StartFlightRecording=disk=true,dumponexit=true,filename=recording.jfr

In JDK 11, which is the first version that doesn't require -XX:+UnlockCommercialFeatures, it is sufficient with:

-XX:StartFlightRecording:filename=recording.jfr
  1. -XX:+FlightRecorder is only needed for releases prior to Oracle JDK 8u40.
  2. In JDK 11, disk is true by default.
  3. If a filename is specified, dumponexit is set to true implicitly.
  4. JDK 11 supports ":" as separator.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.