Shubham Panchal’s Post

[Experiment] How do Spring Boot applications communicate with relational databases and how does one implement a DB driver? Assuming the project is configured to use Spring Data JPA, Hibernate, and PostgreSQL DB: 1. To write clean, maintainable code, you wish to represent DB entities/operations as Java types/functions, respectively. The mapping between abstract DB objects and Java types is provided by the JPA (Java Persistence API) specification. An implementation of the JPA spec is provided by the Hibernate ORM. Parsing queries written in @Query and translating @Entity into SQL tables is performed by Hibernate (or any JPA-compliant ORM). The Spring Data package, providing JpaRepository/CrudRepository interfaces, transfers calls to save(), findAll() to Hibernate implementations. 2. Once the SQL statements are generated by the ORM in step (1), a driver is used to send those SQL statements to the DB server. Each major RDBMS has its own driver. A common specification/interface followed by all drivers is JDBC (Java Database Connectivity). 3. Common RDBMS like MySQL and PostgreSQL do not communicate via HTTP; instead, they use their own messaging formats. The driver encodes/decodes these messages sent/received from the DB server. The driver also exposes a common JDBC API used by the upper ORM package. 4. The driver could either be written in a compiled language like C/C++ (exposing JDBC API via JNI), AKA Type 2 driver, or be written entirely in Java, AKA Type 4 driver. Flow: Spring Data (JpaRepository) -> Hibernate (JPA) -> Driver (JDBC) -> DB Server As depicted in the image below, I have implemented a Type 4 (thin) driver program for PostgreSQL. It authenticates against the DB, sends a query, and displays the result rows, all by constructing binary messages and sending them over a socket. The message types and formats are described in the PostgreSQL docs. Implementing this driver was a great coding exercise and helped me understand how the Java DB stack works as a whole. GitHub: https://lnkd.in/dqUvAjqy #programming #java #databases

  • text

To view or add a comment, sign in

Explore content categories