Return to site

🍃⚡ SPRING BOOT 4.1.0: LAZY JDBC CONNECTIONS + GRPC

· spring

Spring Boot 4.1.0 brings two practical upgrades: lazy JDBC connection fetching and first-class gRPC support. 🚀

Section image

🔸 TL;DR

Spring Boot 4.1.0 can reduce avoidable JDBC pool usage and makes gRPC a native Boot experience—from generated code to security, observability and testing.

🔸 1. FETCH JDBC CONNECTIONS LAZILY

spring:
  datasource:
    url: jdbc:postgresql://localhost/app
    username: app
    password: secret
    connection-fetch: lazy

Boot wraps the pooled DataSource in a LazyConnectionDataSourceProxy. A physical connection is borrowed only when a JDBC statement is executed—not merely when a transaction starts.

Why this matters

In a typical setup today:

  • A transaction starts (e.g. @Transactional method)
  • Spring immediately borrows a connection from the pool
  • Even if the code in that transaction never hits the database (e.g. early return, cache hit, validation error), the connection was still taken from the pool and held for the transaction’s entire duration

This leads to:

  • More connections in use than necessary
  • Larger or more expensive connection pools
  • Higher database load, especially in apps with many read-only or short operations that don’t always query the DB

What lazy JDBC connection fetching does

With lazy fetching:

  • Starting a transaction does not immediately borrow a DB connection
  • The connection is acquired only on first actual JDBC usage (e.g. first JdbcTemplate call, first JPA query, etc.)
  • If no JDBC call happens during the transaction, no connection is ever borrowed

So practically:

  • Fewer connections are in use at the same time
  • The pool can be smaller for the same throughput
  • You reduce contention on the DB and on the connection pool

When this helps

Applications with: Many @Transactional methods that sometimes don’t hit the DB A lot of caching or short‑circuit logic before DB access High concurrency with modest actual DB usage per request

🔸 2. EXPOSE A GRPC SERVICE

What is gRPC?

gRPC is a framework for remote procedure calls that:

  • Uses HTTP/2 under the hood
  • Uses Protocol Buffers (Protobuf) for defining APIs and serializing data (binary, compact, fast)
  • Generates client and server code from .proto files
  • Supports streaming (client, server, bi‑directional streams) and deadlines, metadata, etc.

Instead of sending JSON over HTTP with REST, you’re calling methods like userService.createUser() over the network.

@GrpcService
class GreetingService
    extends GreeterGrpc.GreeterImplBase {
    @Override
    public void greet(
        HelloRequest request,
        StreamObserver<HelloReply> observer) {
            var reply = HelloReply.newBuilder()
                        .setMessage("Hello " + request.getName())
                        .build();
            observer.onNext(reply);
            observer.onCompleted();
    }
}

A generated gRPC implementation exposed as a Spring bean is registered automatically. The default standalone server uses Netty on port 9090.

🔸 3. IMPORT A GRPC CLIENT

@SpringBootApplication
@ImportGrpcClients(
    target = "static://localhost:9090",
    types = GreeterGrpc.GreeterBlockingStub.class
)
class Application {
}

The generated stub becomes an injectable Spring bean. Channels support external configuration, TLS/mTLS, interceptors, observability and in-process testing.

Why use gRPC with Spring Boot?

Compared to plain REST in Spring Boot:

Performance

  1. Binary Protobuf is smaller and faster than JSON
  2. HTTP/2 multiplexing and connection reuse

Strong typing & contracts

  1. .proto files define your API precisely
  2. Client/server code generated from the same contract → fewer mismatches

Streaming support

  1. Server‑streaming, client‑streaming, and bi‑directional streaming are first‑class

Good for internal microservices

  1. Often used for service‑to‑service communication in microservice architectures
  2. You can still expose REST for external clients and gRPC internally

🔸 WHAT ELSE DOES 4.1.0 BRING?

▪️ Netty or Servlet-based gRPC over HTTP/2

▪️ Maven and Gradle Protocol Buffers integration

▪️ Better Jackson read/write and factory configuration

▪️ InetAddressFilter support for SSRF mitigation

▪️ @Async observability context propagation

▪️ Log4j rotation by size, time, both or cron

▪️ JWT authorities extracted through SpEL

▪️ Spring Batch metadata stored in MongoDB

▪️ @RedisListener auto-configuration

⚠️ Upgrade note: APIs deprecated in Spring Boot 4.0 were removed. Derby integration is deprecated, layertools mode is gone, and jOOQ 3.20 requires Java 21.

🔸 TAKEAWAYS

▪️ Use lazy fetching when transactions may execute no SQL.

▪️ Measure pool usage and latency before and after enabling it.

▪️ Choose gRPC for strongly typed service-to-service communication—not as an automatic REST replacement.

▪️ Review removals and Java requirements before upgrading.

Which feature matters most to you: lazy JDBC connections or gRPC? 👇

#SpringBoot #SpringFramework #Java #gRPC #JDBC #Microservices #BackendDevelopment #SoftwareArchitecture #OpenTelemetry