A Cruncher Choice JPA or JDBC

Deependra Chourasia
Walmart Global Tech Blog
4 min readApr 1, 2022

Your system is the result of your choices.

One of the most important steps to take when it comes to developing a successful backend is to pick the right tech stack. Why? Because creating a product is not just about a functional design but also a secure, stable, and expeditious design.

Let’s dive deep to settle on a framework for Database operations…

What is JDBC?

Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language that defines how a client can access a database.

In February 1997, Sun Microsystems released JDBC as a part of the JDK. Programmers can use these standard interfaces and classes to write applications that connect with databases, send SQL queries, and process the results.

We can say that JDBC is the bridge between the Java world and the database world. After all, the first thing we look for when we want to connect a database to our application is the JDBC driver.

Pros of JDBC

  • Simple SQL processing
  • Good performance with large data
  • Very good for applications where every zeptosecond counts
  • Simple syntax so easy to learn
  • It is useful in applications where full control of the execution is required.

Cons of JDBC

  • Large programming overhead
  • There is no encapsulation
  • Hard to implement the MVC concept
  • Queries are DBMS specific

What is JPA?

JPA is a Java standard for binding Java objects to records in a relational database. We can say it's one of the possible solutions for ORM, with this developers can perform CRUD operations on Relational Databases using Java Objects(POJO).

The first version of Java Persistence API, JPA 1.0 was released in 2006 as a part of the EJB 3.0 specification with the motive to reduce the boilerplate code for Object Mapping and hide SQL from the developer.

Implementations of the JPA specification are available from many vendors, such as Hibernate, Toplink, iBatis, OpenJPA, etc. Under the hood, Hibernate and most other providers for JPA uses JDBC to read and write from and to the DB.

It's a tough decision for System Architect to settle on one to communicate with the Back-End database as JPA and JDBC have very different approaches for working with the persistence layer.

JPA and JDBC differ primarily in their level of abstraction. JDBC is a low-level standard for interaction with databases. JPA is a higher-level standard for the same purpose. With JDBC you will have to write all the SQL stuff, validations, ORM, transaction management. On the other hand, you can code directly with business logic using JPA as JPA will handle all the SQL stuff and ORM tasks.

Pros of JPA

  • Provides encapsulation which lets developers focus on business logic.
  • Integration with java beans validation for Easier Validations
  • Provides Database Independence
  • A single request manages all the SQL in a single transaction.

Cons of JPA

  • Not good for batch transactions. Consumes too much memory.
  • Not thread-safe.
  • A better understanding of what happens is required to specify additional fetches and improve performance.
  • JPA is 4x slower than JDBC when it comes to large-batch processing.
  • Mapping between database tables can be a bit challenging.

Performance comparison of Spring Data-JPA and Spring-Data-JDBC …

I created demo applications to evaluate and understand two modules provided by Spring Data Framework.

  1. Spring-Data-JDBC: https://github.com/rkDeependra/Spring-JDBC-Demo
  2. Spring-Data-JPA: https://github.com/rkDeependra/Spring-JPA-Demo

Did a performance analysis of JPA and JDBC modules by Spring-Data, where I found out that JDBC outperforms JPA by a significant factor in one-by-one inserts and by a margin in batch inserts.

Considering JPA’s offerings, this difference is considerable if implemented with the attention to the details of the flow & used according to the need.

graphical comparison for JPA and JDBC sequential inserts
JBDC is ~20% faster than JPA
graphical comparison for JPA and JDBC batch inserts
JDBC is ~4% faster than JPA

Records were inserted synchronously.

Batch size used in analysis is 100

The database used in the performance analysis is Microsoft Azure-SQL.

SQL table was created with auto-increment SQL generated id. (IDENTITY)

Summing-up

JPA’s ORM mapping is its major advantage over JDBC because it converts object-oriented Java code to the back-end database without needing time-consuming, error-prone coding.

But JDBC allows you to do more things with the Database directly & give more control.

There is a trade-off between coding time and fine-grained control.

Before finalizing a framework, one must engineer and understand the future requirements of the application and the Uses.

Further Read

--

--