diff --git a/README.md b/README.md
index 0919436cf..a5466aed2 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@ The following table list all sample codes related to the spring boot integration
| [Hibernate Envers Implementation using spring data JPA](./jpa/boot-data-envers) | The application, demonstrates how to apply hibernate envers to the spring boot project, monitor the system and alert when CPU usage is high or when system is down | Completed |
| [Graph QL implementation using webflux](./graphql/boot-graphql-webflux) | The application, demonstrates the way to connect to database using graph ql using webflux | Completed |
| [Hibernate 2nd Level Cache Using Redis](./jpa/boot-hibernate2ndlevelcache-sample) | The application, demonstrates how to apply Hibernate 2nd level cache using redis in a spring boot project , testing using QueryCounting, implemented hypersistence Repository instead of default JPARepository | Completed |
-| [Read Replica Postgres](./jpa/boot-read-replica-postgresql) | The application, demonstrates saving the data in Postgresql and then read from replica instance | Completed |
+| [Read Replica Postgres with connection optimization](./jpa/boot-read-replica-postgresql) | The application, demonstrates saving the data in Postgresql and then read from replica instance with optimized connection handling via LazyConnectionDataSourceProxy | Completed |
| [BackgroundJobs and Scheduling using Jobrunr](./scheduler/boot-scheduler-jobrunr) | The application, demonstrates running background jobs and scheduling the tasks using [Jobrunr](https://www.jobrunr.io/en/) | Completed |
| [MultiTenancy DB Based](./jpa/multitenancy/multitenancy-db) | The application, demonstrates running multi tenancy in JPA using different databases but same DDLs and DMLs | Completed |
| [MultiTenancy Partition Based](./jpa/multitenancy/partition) | The application, demonstrates running multi tenancy in JPA using partition based i.e Shared Database with Shared table | Completed |
diff --git a/jpa/boot-read-replica-postgresql/README.md b/jpa/boot-read-replica-postgresql/README.md
index a5183405e..45fdbd419 100644
--- a/jpa/boot-read-replica-postgresql/README.md
+++ b/jpa/boot-read-replica-postgresql/README.md
@@ -4,6 +4,7 @@ This project is an example to show how we can separate read and write operations
A read replica in Postgres is a database instance that receives data from a primary database instance and serves it to clients. Read replicas are useful for scaling database workloads, as they can offload read operations from the primary instance, allowing it to focus on more resource-intensive tasks such as writing data. This can improve the performance of the overall database system. Read replicas can also be useful for providing high availability, as they can take over read operations if the primary instance becomes unavailable for any reason.
- All reads will go to reader instance and writes will go to writer instance
+ - Swithching between master and replica can be observed at docker compose logs or in application/logs when datasourceproxy is enabled.
![](../../images/replica.png)
diff --git a/jpa/boot-read-replica-postgresql/docker-compose.yaml b/jpa/boot-read-replica-postgresql/docker-compose.yaml
index 3f77dcb03..3e7644958 100644
--- a/jpa/boot-read-replica-postgresql/docker-compose.yaml
+++ b/jpa/boot-read-replica-postgresql/docker-compose.yaml
@@ -1,5 +1,3 @@
-version: '2'
-
services:
postgresql-master:
image: 'bitnami/postgresql:latest'
diff --git a/jpa/boot-read-replica-postgresql/pom.xml b/jpa/boot-read-replica-postgresql/pom.xml
index 9fd7d1786..4e8019971 100644
--- a/jpa/boot-read-replica-postgresql/pom.xml
+++ b/jpa/boot-read-replica-postgresql/pom.xml
@@ -5,7 +5,7 @@
org.springframework.bootspring-boot-starter-parent
- 3.3.5
+ 3.4.0-RC1com.example.demo
@@ -50,12 +50,16 @@
org.liquibaseliquibase-core
-
org.postgresqlpostgresqlruntime
+
+ net.ttddyy.observation
+ datasource-micrometer-spring-boot
+ 1.0.5
+ org.springframework.boot
@@ -105,7 +109,7 @@
- 1.22.0
+ 1.24.0
diff --git a/jpa/boot-read-replica-postgresql/src/main/java/com/example/demo/readreplica/config/DatabaseConfig.java b/jpa/boot-read-replica-postgresql/src/main/java/com/example/demo/readreplica/config/DatabaseConfig.java
index 2b69b19af..862496cc2 100644
--- a/jpa/boot-read-replica-postgresql/src/main/java/com/example/demo/readreplica/config/DatabaseConfig.java
+++ b/jpa/boot-read-replica-postgresql/src/main/java/com/example/demo/readreplica/config/DatabaseConfig.java
@@ -1,15 +1,13 @@
package com.example.demo.readreplica.config;
-import com.example.demo.readreplica.config.routing.RoutingDataSource;
import com.zaxxer.hikari.HikariDataSource;
-import java.util.HashMap;
-import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
+import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
@Configuration(proxyBeanMethods = false)
class DatabaseConfig {
@@ -24,7 +22,7 @@ DataSourceProperties primaryDataSourceProperties() {
}
@Bean
- @ConfigurationProperties(PRIMARY_DATABASE_PROPERTY_KEY_PREFIX + ".configuration")
+ @ConfigurationProperties(PRIMARY_DATABASE_PROPERTY_KEY_PREFIX + ".hikari")
DataSource primaryDataSource(final DataSourceProperties primaryDataSourceProperties) {
return primaryDataSourceProperties
.initializeDataSourceBuilder()
@@ -39,7 +37,7 @@ DataSourceProperties replicaDataSourceProperties() {
}
@Bean
- @ConfigurationProperties(REPLICA_DATABASE_PROPERTY_KEY_PREFIX + ".configuration")
+ @ConfigurationProperties(REPLICA_DATABASE_PROPERTY_KEY_PREFIX + ".hikari")
DataSource replicaDataSource(final DataSourceProperties replicaDataSourceProperties) {
return replicaDataSourceProperties
.initializeDataSourceBuilder()
@@ -50,15 +48,9 @@ DataSource replicaDataSource(final DataSourceProperties replicaDataSourcePropert
@Bean
@Primary
DataSource dataSource(final DataSource primaryDataSource, final DataSource replicaDataSource) {
- final RoutingDataSource routingDataSource = new RoutingDataSource();
-
- final Map