Spring Data JPA: Deep Dive

SpringBoot with Spring JDBC:

When we add the Spring JDBC dependency, it defaults to using HikariCP as the connection pool implementation. Other connection pool options include C3P0, Apache DBCP2, and others.

To interact with the database, we use JdbcTemplate, and we configure the database properties in the application.properties or application.yml file. These properties help Spring Boot automatically configure a DataSource that manages the database connections.

If we disable auto-configuration or exclude specific auto-configuration classes, we need to manually configure these settings. To disable automatic configuration, we can use:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class})

For testing purposes, we can use DriverManagerDataSource. However, it’s not recommended for production use because DriverManagerDataSource does not support connection pooling, meaning it will open a new connection for each query, which can significantly impact performance.

JPA Architecture:

JDBC is an Interface actually work done by Driver we can choose what driver we want acc to db.

JPA and JDBC is to maintain relational Databases

using JDBC with Springboot:

Problem with this

Every time we are creating one jdbc it is not ideal way we have to create a connection pool

Using jdbc with springboot:

@Repository → is it used as component and to handle granular level of any sql expection

DataSource helps in spring boot are one is create a connection and return connecton and manage connection pool

Springboot also provide by default HikcariConnection Pool

org.springframework.dao.package → it has all exception provides and jdbc template mapping it all internally

it has max pool size and min is10

if you want to use any specific connection pool then use can use this connection pool in this DataSource bean Instead of providing in application.properties

JDBC frequency use methods

For complex sql query you can use preapared statement

Jpa LifeCycle:

Life cycle of peristance context:

JPA Architecture:

Spring JPA First Level Caching:

here in tthe read-Jap fotrst time ot query and next findById it will cache

this EntityManager.persist() impl provided by the hibernate and first it creates peristance context and it will goes to db unttil commit that is flush()

1 em → 1pc

Session Impl is the hibernate Impl for persistence context for entity manager

How it goes to same entity manager or peristence context;

One http reuqest mean on e desiplacher servlet and one entity manager an don eperistence context

and if you send new http request means new entit manager and new peristence context

@Transcational Annotation:

@EnableTranscationalManagement we can enable or by default spring uses auto configuration

We can apply for class level and method level if we apply as class level it apply to all public methods only

This transactional management in springboot uses springAOP

→ uses pointcut Expression to search expression, which has @Transcational like @Within

→once point cut expression matches, run as Around type advice, Advice is invokeWithinTranscation present in the Transcationalnterceptor Method

Spring Data JPA L2 caching:

this cache annotation represent when ever who are working with this entity first it will check it peristance context if not present then it goes to the L2 cache