Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polish : Hibernate2nd level cache #1599

Merged
merged 4 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jpa/boot-hibernate2ndlevelcache-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>net.ttddyy</groupId>
<artifactId>datasource-proxy</artifactId>
<version>1.10.1</version>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
<version>1.0.6</version>
Comment on lines +109 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Configuration needs to be updated for micrometer-based observability

The verification revealed that while the dependency has been updated to datasource-micrometer-spring-boot, the configuration still contains old datasource-proxy related code:

  • JpaConfiguration.java still imports and uses net.ttddyy.dsproxy classes
  • application.properties contains outdated proxy configuration properties starting with jdbc.datasource-proxy

These need to be replaced with the appropriate micrometer configuration. Here's what needs to be changed:

  • Remove the QueryExecutionListener bean and related imports from JpaConfiguration.java
  • Remove all jdbc.datasource-proxy.* properties from application.properties
  • Add appropriate micrometer properties if needed (though the basic setup should work out of the box with Spring Boot auto-configuration)
🔗 Analysis chain

LGTM! Good move to micrometer-based observability.

The switch from datasource-proxy to datasource-micrometer-spring-boot aligns well with modern Spring Boot observability patterns. This change will provide better integration with Spring Boot's metrics and observability stack.

Run the following script to verify the integration:


Let me verify the configuration in the specific project. I'll check for any remaining proxy configuration and the proper setup for the new micrometer integration.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the removal of old proxy configuration and presence of new micrometer configuration

# Check if there are any remaining references to the old datasource-proxy
rg -l "net\.ttddyy\.dsproxy"

# Check for proper micrometer configuration
fd -e properties -e yml -e yaml | xargs rg "management\.metrics|management\.endpoints\.web\.exposure"

Length of output: 6702


Script:

#!/bin/bash
# Check the JpaConfiguration file in the hibernate2ndlevelcache sample for any remaining proxy config
cat jpa/boot-hibernate2ndlevelcache-sample/src/main/java/com/example/hibernatecache/config/JpaConfiguration.java

# Check application.properties for micrometer configuration
cat jpa/boot-hibernate2ndlevelcache-sample/src/main/resources/application.properties

Length of output: 3181

</dependency>

<dependency>
Expand Down Expand Up @@ -266,7 +266,7 @@
<configuration>
<java>
<palantirJavaFormat>
<version>2.47.0</version>
<version>2.50.0</version>
</palantirJavaFormat>
<importOrder />
<removeUnusedImports />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

@SpringBootApplication
@EnableConfigurationProperties({ApplicationProperties.class})
public class Application {
public class Hibernate2ndLevelCacheApplication {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(Hibernate2ndLevelCacheApplication.class, args);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

import com.example.hibernatecache.repositories.CustomerRepository;
import io.hypersistence.utils.spring.repository.BaseJpaRepositoryImpl;
import net.ttddyy.dsproxy.listener.DataSourceQueryCountListener;
import net.ttddyy.dsproxy.listener.QueryExecutionListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories(basePackageClasses = CustomerRepository.class, repositoryBaseClass = BaseJpaRepositoryImpl.class)
@Configuration(proxyBeanMethods = false)
class JpaConfiguration {}
class JpaConfiguration {

@Bean
QueryExecutionListener queryExecutionListener() {
return new DataSourceQueryCountListener();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.example.hibernatecache.entities.Customer;
import io.hypersistence.utils.spring.repository.BaseJpaRepository;
import io.hypersistence.utils.spring.repository.HibernateRepository;
import jakarta.persistence.QueryHint;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
Expand All @@ -15,9 +14,7 @@
import org.springframework.transaction.annotation.Transactional;

public interface CustomerRepository
extends BaseJpaRepository<Customer, Long>,
HibernateRepository<Customer>,
PagingAndSortingRepository<Customer, Long> {
extends BaseJpaRepository<Customer, Long>, PagingAndSortingRepository<Customer, Long> {

@Transactional(readOnly = true)
@QueryHints(@QueryHint(name = HINT_CACHEABLE, value = "true"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import com.example.hibernatecache.entities.OrderItem;
import io.hypersistence.utils.spring.repository.BaseJpaRepository;
import io.hypersistence.utils.spring.repository.HibernateRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface OrderItemRepository
extends BaseJpaRepository<OrderItem, Long>,
HibernateRepository<OrderItem>,
PagingAndSortingRepository<OrderItem, Long> {
extends BaseJpaRepository<OrderItem, Long>, PagingAndSortingRepository<OrderItem, Long> {

void deleteAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.example.hibernatecache.entities.Order;
import io.hypersistence.utils.spring.repository.BaseJpaRepository;
import io.hypersistence.utils.spring.repository.HibernateRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface OrderRepository
extends BaseJpaRepository<Order, Long>, HibernateRepository<Order>, PagingAndSortingRepository<Order, Long> {
public interface OrderRepository extends BaseJpaRepository<Order, Long>, PagingAndSortingRepository<Order, Long> {

void deleteAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ spring.cache.type=redis
spring.data.redis.host=localhost
spring.data.redis.port=6379


########### datasource micrometer ###########
jdbc.datasource-proxy.logging=slf4j
jdbc.datasource-proxy.slow-query.logger-name=slow-query-logger
jdbc.datasource-proxy.query.logger-name=query-logger
jdbc.datasource-proxy.query.enable-logging=true
jdbc.datasource-proxy.slow-query.enable-logging=true
jdbc.datasource-proxy.include-parameter-values=true
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@

<logger name="com.example.hibernatecache" level="DEBUG"/>
<logger name="org.springframework" level="INFO"/>
<logger name="query-logger" level="DEBUG" />
<logger name="slow-query-logger" level="WARN" />

</configuration>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.hibernatecache;

import static io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator.assertInsertCount;
import static io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator.assertSelectCount;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand Down Expand Up @@ -32,9 +30,10 @@ void contextLoads() throws Exception {
.andExpect(jsonPath("$.lastName", is(customerRequest.lastName())))
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.phone", is(customerRequest.phone())));
assertInsertCount(1);
SQLStatementCountValidator.assertInsertCount(1);
// For selecting next sequence value
assertSelectCount(1);
SQLStatementCountValidator.assertSelectCount(1);
SQLStatementCountValidator.assertTotalCount(2);
SQLStatementCountValidator.reset();
for (int i = 0; i < 10; i++) {
this.mockMvc
Expand All @@ -46,6 +45,7 @@ void contextLoads() throws Exception {
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.phone", is(customerRequest.phone())));
}
assertSelectCount(1);
SQLStatementCountValidator.assertSelectCount(1);
SQLStatementCountValidator.assertTotalCount(1);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.hibernatecache;

import com.example.hibernatecache.common.ContainersConfig;
import org.springframework.boot.SpringApplication;

public class TestHibernate2ndLevelCacheApplication {

public static void main(String[] args) {
SpringApplication.from(Hibernate2ndLevelCacheApplication::main)
.with(ContainersConfig.class)
.withAdditionalProfiles("local")
.run(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.hibernatecache.web.controllers;

import static io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator.assertInsertCount;
import static io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator.assertSelectCount;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.empty;
Expand Down Expand Up @@ -104,9 +102,10 @@ void shouldFindCustomerById() throws Exception {
.andExpect(jsonPath("$.phone", is(customer.getPhone())))
.andExpect(jsonPath("$.orders.size()", is(1)));

assertInsertCount(0);
SQLStatementCountValidator.assertInsertCount(0);
// For selecting customer and order
assertSelectCount(2);
SQLStatementCountValidator.assertSelectCount(2);
SQLStatementCountValidator.assertTotalCount(2);
}

@Test
Expand All @@ -126,9 +125,10 @@ void shouldFindCustomerByFirstname() throws Exception {
.andExpect(jsonPath("$.phone", is(customer.getPhone())))
.andExpect(jsonPath("$.orders.size()", is(1)));

assertInsertCount(0);
SQLStatementCountValidator.assertInsertCount(0);
// For selecting customer and then orderItems
assertSelectCount(2);
SQLStatementCountValidator.assertSelectCount(2);
SQLStatementCountValidator.assertTotalCount(2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

<logger name="org.testcontainers" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="query-logger" level="DEBUG" />
<logger name="slow-query-logger" level="WARN" />
</configuration>
Loading